* This dofile prepares the final database used in the analysis.

// Inputs: one dataset per election type, where district identifiers are modified to account for redistricting over time (leg_database_redistrict and cant_database_redistrict)
// Part 0: some preparatory cleaning of the database, including merging with unique candidate IDs, textual outcomes and expenditure data
// Part 1: construction of 'assignment' databases for 3 definitions of treatment: winning the election, qualifying a third candidate for the runoff, qualifying for the runoff over the next candidate
// Part 2: preparation of descriptive variables at the district level, at time t, t-1 and t+1
// Part 3: preparation of outcomes at the political orientation level, at time t, t-1 and t+1
// Part 4: preparation of outcomes at the party and coalition level, at time t, t-1 and t+1
// Part 5: preparation of outcomes at the individual level, at time t, t-1 and t+1
// Part 6: merge each assignment dataset with the outcome datasets at t, t-1 and t+1

********************************************************************
*** 0/ INITIAL DATABASE AT CANDIDATE LEVEL
********************************************************************

use "$intermediate/initial_dataset_cand_lvl", clear

// Merge with candidate ID's linked across election years
rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$intermediate/dataset_cand_id_across_elections_new"
assert _merge==3
drop _merge

count if (first_name_cand!=_first_name_cand) | (last_name_cand!=_last_name_cand)
assert r(N)==0

// Add id_unique_cand_next2 to identify candidates two periods earlier
tempfile temp
save `temp'

keep if id_unique_cand_prev!=. & id_unique_cand_next!=.
gen id_unique_cand_next2=id_unique_cand_next
replace id_unique_cand=id_unique_cand_prev
keep id_unique_cand id_unique_cand_next2

merge 1:1 id_unique_cand using `temp'
drop _merge

// Export list of identifiers with name and district to merge with other datasets
tempfile temp
save `temp'

keep election_type id_district year departement_code constituency_code canton_code last_name_cand first_name_cand political_label_cand id_unique_cand id_unique_cand_prev id_unique_cand_next id_unique_cand_next2
save "$intermediate/initial_dataset_withid", replace

use `temp', clear

// Redefine orientation so it is more tractable
rename political_orientation_cand orientation
replace orientation="fl" if orientation=="farleft"
replace orientation="l" if orientation=="left"
replace orientation="c" if orientation=="center"
replace orientation="r" if orientation=="right"
replace orientation="fr" if orientation=="farright"
replace orientation="nc" if orientation=="nonclassified"
count if (orientation=="" & political_label!="") | (orientation!="" & political_label=="")
assert r(N)==0

// Redefine nuance to track it over time 
rename party_cand realparty_cand //variable that indicates if the candidate is affiliated with a party 
rename party_name party //historical lineage
ta party
replace party="RG" if party=="RadGauche" //shorten name to avoid later issues in variable names

// Modification for 2 nuances that are considered as independents but that we label as parties
ta political_label_cand year if realparty_cand==0 & party!=""
replace realparty_cand=1 if political_label_cand=="CEN" & year==2012
replace realparty_cand=1 if political_label_cand=="ECO" & year==2017

// Check definition of coalitions
count if party!="" & (coalition1=="" | coalition2=="")
assert r(N)==0 
count if party!="" & coalition1==""
assert r(N)==0 

// Dummy for holding a second round in the disrict 
byso id_unique: egen temp=max(ranking_cand_R2)
gen secround_district=temp!=.
drop temp
label var secround_district "district with a second round"

// Dummies for qualifying to second round, running in second round and winning the election 
count if nb_registered_R1==.
//a few districts with nb of registered missing
gen qualif_cand=0 if prop_registered_votes_cand_R1!=.
replace qualif_cand=1 if prop_registered_votes_cand_R1>=0.125 & prop_registered_votes_cand_R1!=. & ((election_type==1 & year>=1978) | (election_type==2 & year>=2011)) 
replace qualif_cand=1 if prop_registered_votes_cand_R1>=0.1 & prop_registered_votes_cand_R1!=. & ((election_type==1 & year<1978 & year>=1967) | (election_type==2 & year<2011)) 
replace qualif_cand=1 if prop_voters_votes_cand_R1>=0.05 & prop_voters_votes_cand_R1!=. & (election_type==1 & year<1967) 
//NB: in 1958 and 1962 the threshold is 5% of valid votes, not registered voters

// Qualifying when the second candidate does not pass the threshold
gen secround_type=1 //type of district: 1 = the second passes the threshold and 2 = the second does not pass the threshold
gen temp=prop_registered_votes_cand_R1 if ranking_cand_R1==2
byso id_unique: egen temp2=min(temp) //share of registered voters received by second cand
replace secround_type=2 if temp2<0.125 & ((election_type==1 & year>=1978) | (election_type==2 & year>=2011)) 
replace secround_type=2 if temp2<0.1 & ((election_type==1 & year<1978 & year>=1967) | (election_type==2 & year<2011)) 
drop temp*
gen temp=prop_voters_votes_cand_R1 if ranking_cand_R1==2
byso id_unique: egen temp2=min(temp) //share of votes received by second cand
replace secround_type=2 if temp2<0.05 & (election_type==1 & year<1967) 
drop temp*

replace qualif_cand=1 if secround_type==2 & ranking_cand_R1<=2 //qualifying because ranked first or second

byso id_unique (ranking_cand_R1): gen temp=prop_registered_votes_cand_R1==prop_registered_votes_cand_R1[_n-1] if ranking_cand_R1==3 //dummy for the second and third candidate receiving the same vote share
li year id_district id_unique election_type prop_registered_votes_cand_R1 prop_registered_votes_cand_R2 qualif_cand if temp==1
replace qualif_cand=1 if temp==1 //7 cases of third candidate qualified because they got the same vote share as the second one
drop temp

replace qualif_cand=. if secround_district==0
replace secround_type=. if secround_district==0 //define these variables only if there is indeed a second round

label var qualif_cand "qualifying for second round"
label var secround_type "type of second round (second-ranked passes threshold or not)"

gen secround_cand=ranking_cand_R2!=. 
label var secround_cand "running in the second round"

gen win_cand=((secround_district==1 & ranking_cand_R2==1) | (secround_district==0 & ranking_cand_R1==1))
label var win_cand "election won by candidate"

gen win_cand_R1=(secround_district==0 & ranking_cand_R1==1)
label var win_cand_R1 "election won by candidate, round 1"
gen win_cand_R2=(secround_district==1 & ranking_cand_R2==1)
label var win_cand_R2 "election won by candidate, round 2"

// Number of candidates in each round
byso id_unique: gen nb_cands_R1=_N
label var nb_cands_R1 "# candidates, round 1"

byso id_unique: egen nb_cands_R2=total(secround_cand)
label var nb_cands_R2 "# candidates, round 2"

ta qualif_cand secround_cand 
li year id_district id_unique election_type ranking_cand_R1 prop_registered_votes_cand_R1 if qualif_cand==0 & secround_cand==1
//NB: 7 cases of candidates who run in the second round when on paper they should not
gen flag_secround_cand=qualif_cand==0 & secround_cand==1
label var flag_secround_cand "candidate runs in second round without qualifying on paper"

// Time period
tempfile temp
save `temp' 

byso id_unique: keep if _n==1

byso election_type id_district (year): gen temp=year-year[_n-1]
byso election_type id_district (year): gen temp2=year[_n-1]
ta temp if election_type==1
ta year if temp==1 & election_type==1
ta year if temp==2 & election_type==1
ta year if temp==3 & election_type==1
ta year if temp==4 & election_type==1
ta year if temp==5 & election_type==1 //ok, all legislative elections are held in due time

ta temp if election_type==2
ta year temp2 if temp==2 & election_type==2 //some cantons have elections in both 1992 and 1994, i.e off-cycle
ta year temp2 if temp==3 & election_type==2 //same thing
ta year temp2 if temp==4 & election_type==2 //same thing
//no district has 'more' years than expected between two elections
drop temp*

byso election_type id_district (year): gen id_time=_n
byso election_type id_district (year): gen nomerge_prev=_n==1 //indicator for not having any obs at t-1
byso election_type id_district (year): gen nomerge_prev2=_n<=2 //indicator for not havingany obs at t-2
byso election_type id_district (year): gen nomerge_next=_n==_N //indicator for not having any obs at t+1
label var nomerge_prev "No observation at time t-1"
label var nomerge_prev2 "No observation at time t-2"
label var nomerge_next "No observation at time t+1"

keep id_unique id_time nomerge*

merge 1:m id_unique using `temp'
drop _merge


// Count the number of times a candidate runs and wins (in same district)
tempfile temp
save `temp', replace

use `temp', clear
keep if id_time==1
keep if id_unique_cand_next!=.
gen id_merge=id_unique_cand_next
replace id_time=2
keep id_time id_merge
tempfile tempmerge
save `tempmerge', replace

use `temp', clear
rename id_unique_cand id_merge
merge 1:1 id_time id_merge using `tempmerge'
gen nb_run_cand=_merge==3 if nomerge_prev==0 & id_time==2
drop _merge
rename id_merge id_unique_cand 
save `temp', replace

forval i=2/7{
use `temp', clear
keep if id_time==`i'
keep if id_unique_cand_next!=.
gen id_merge=id_unique_cand_next
replace id_time=`i'+1
gen toadd=nb_run_cand
keep id_time id_merge toadd
tempfile tempmerge
save `tempmerge', replace

use `temp', clear
rename id_unique_cand id_merge
merge 1:1 id_time id_merge using `tempmerge'
assert _merge==1 if nomerge_prev==1
replace nb_run_cand=(_merge==3) if nomerge_prev==0 & id_time==`i'+1
assert toadd==. if _merge!=3
replace nb_run_cand=nb_run_cand+toadd if toadd!=.
drop _merge toadd
rename id_merge id_unique_cand 
save `temp', replace
}


tempfile temp
save `temp', replace

// Winning before previous election
use `temp', clear
keep if id_time==1 & win_cand==1
keep if id_unique_cand_next2!=.
gen id_merge=id_unique_cand_next2
replace id_time=3
keep id_time id_merge
tempfile tempmerge
save `tempmerge', replace

use `temp', clear
rename id_unique_cand id_merge
merge 1:1 id_time id_merge using `tempmerge'
gen nb_win_cand=_merge==3 if nomerge_prev2==0 & id_time==3
drop _merge
rename id_merge id_unique_cand 
save `temp', replace

forval i=2/6{
use `temp', clear
keep if id_time==`i' & win_cand==1
keep if id_unique_cand_next2!=.
gen id_merge=id_unique_cand_next2
replace id_time=`i'+2
gen toadd=nb_win_cand
keep id_time id_merge toadd
tempfile tempmerge
save `tempmerge', replace

use `temp', clear
rename id_unique_cand id_merge
merge 1:1 id_time id_merge using `tempmerge'
assert _merge==1 if nomerge_prev2==1
replace nb_win_cand=(_merge==3) if nomerge_prev2==0 & id_time==`i'+2
assert toadd==. if _merge!=3
replace nb_win_cand=nb_win_cand+toadd if toadd!=.
drop _merge toadd
rename id_merge id_unique_cand 
save `temp', replace
}

// Dummies for running and winning in the past
gen rerun_cand=nb_run_cand>0 if nb_run_cand!=.
gen rerun2_cand=nb_run_cand>1 if nb_run_cand!=.

gen win2_cand=nb_win_cand>0 if nb_win_cand!=.

// Add campaign expenditure data
tempfile temp1 temp2 
save `temp1', replace

use "$raw_data/elections/elections_depenses_cantleg_bycandidates.dta", clear
tostring(id_district), replace
replace political_label_cand="" if first_name_cand=="NA" & last_name_cand=="TENAND" //small adjustment for one candidate
save `temp2', replace

use `temp1'
merge 1:1 year id_unique election_type gender_cand last_name_cand first_name_cand political_label using `temp2', keepusing(tot_expenditures_cand tot_contributions_cand donations_cand personal_contributions_cand party_contributions_cand)
drop if _merge==2 //observations in campaign election file that are not in our dataset due to using different electoral rules
drop _merge 

rename tot_expenditures_cand totexp_indiv
label var totexp_indiv "total expenditures of candidate"
rename tot_contributions_cand totcontrib_indiv
label var totcontrib_indiv "total contributions of candidate"
rename donations_cand donations_indiv
label var donations_indiv "total private donations of candidate"
rename personal_contributions_cand pecontrib_indiv
label var pecontrib_indiv "total personal contributions of candidate"
rename party_contributions_cand pacontrib_indiv
label var pacontrib_indiv "total party contributions of candidate"

count if totexp_indiv==.a | totexp_indiv==.b  //replace expenditure variables taking value .b/.a by a missing value 

local var totexp_indiv totcontrib_indiv donations_indiv pecontrib_indiv pacontrib_indiv
foreach x of varlist `var'{
	replace `x'=. if `x'==.a | `x'==.b
}	

tempfile temp1
save `temp1', replace

// Similarity to other candidates from party
insheet using "$intermediate/df_lsi_party_1962.csv", clear
drop v1
rename mean_similarity_party lsi_party
tostring id_district, replace
tempfile toappend
save `toappend', replace

foreach x in "67" "68" "73" "78" "81" "93" "97"{
insheet using "$intermediate/df_lsi_party_19`x'.csv", clear
drop v1
rename mean_similarity_party lsi_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace
}

insheet using "$intermediate/df_lsi_party_2017.csv", clear
drop v1
rename mean_similarity_party lsi_party

tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace

use `temp1', clear

merge 1:1 year id_unique_cand using `toappend'
assert _merge!=2
drop _merge

tempfile temp1
save `temp1', replace

insheet using "$intermediate/df_similarity_party_1962.csv", clear
drop v1
rename mean_similarity_party sim_party
rename mean_similarity_party_tf simtf_party
tostring id_district, replace
tempfile toappend
save `toappend', replace

foreach x in "67" "68" "73" "78" "81" "93" "97"{
insheet using "$intermediate/df_similarity_party_19`x'.csv", clear
drop v1
rename mean_similarity_party sim_party
rename mean_similarity_party_tf simtf_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace
}

insheet using "$intermediate/df_similarity_party_2017.csv", clear
drop v1
rename mean_similarity_party sim_party
rename mean_similarity_party_tf simtf_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace

use `temp1', clear

merge 1:1 year id_unique_cand using `toappend'
assert _merge!=2
drop _merge

tempfile temp1
save `temp1', replace

insheet using "$intermediate/df_lsi_party_big_1962.csv", clear
drop v1
rename mean_similarity_party lsibig_party
tostring id_district, replace
tempfile toappend
save `toappend', replace

foreach x in "67" "68" "73" "78" "81" "93" "97"{
insheet using "$intermediate/df_lsi_party_big_19`x'.csv", clear
drop v1
rename mean_similarity_party lsibig_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace
}

insheet using "$intermediate/df_lsi_party_big_2017.csv", clear
drop v1
rename mean_similarity_party lsibig_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace

use `temp1', clear

merge 1:1 year id_unique_cand using `toappend'
assert _merge!=2
drop _merge

tempfile temp1
save `temp1', replace

insheet using "$intermediate/df_similarity_party_big_1962.csv", clear
drop v1
rename mean_similarity_party simbig_party
rename mean_similarity_party_tf simbigtf_party
tostring id_district, replace
tempfile toappend
save `toappend', replace

foreach x in "67" "68" "73" "78" "81" "93" "97"{
insheet using "$intermediate/df_similarity_party_big_19`x'.csv", clear
drop v1
rename mean_similarity_party simbig_party
rename mean_similarity_party_tf simbigtf_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace
}

insheet using "$intermediate/df_similarity_party_big_2017.csv", clear
drop v1
rename mean_similarity_party simbig_party
rename mean_similarity_party_tf simbigtf_party
tostring id_district, replace
append using `toappend'
tempfile toappend
save `toappend', replace

use `temp1', clear

merge 1:1 year id_unique_cand using `toappend'
assert _merge!=2
drop _merge

// Index of originality from party
foreach x in "sim_party" "simtf_party" "simbig_party" "simbigtf_party" "lsi_party" "lsibig_party"{
byso year: egen temp=mean(`x')
byso year: egen temp2=sd(`x')
gen sd_`x'=(`x'-temp)/temp2 
drop temp*
}
egen temp=rsum(sd_*), missing
gen originality_indiv=-temp/6 //negative sign because originality = -similarity
label var originality_indiv "originality of candidate"

drop temp sd_*

tempfile temp1
save `temp1', replace

// Personal pronouns and past participles
foreach y in "perso" "vpp"{
insheet using "$intermediate/df_`y'_1962.csv", clear
drop v1
keep if tour==1
drop tour
gen year=1962
tempfile toappend
save `toappend', replace

foreach x in "67" "68" "73" "78" "81" "93" "97"{
insheet using "$intermediate/df_`y'_19`x'.csv", clear
drop v1
keep if tour==1
drop tour
gen year=19`x'
append using `toappend'
tempfile toappend
save `toappend', replace
}

insheet using "$intermediate/df_`y'_2017.csv", clear
drop v1
keep if tour==1
drop tour
gen year=2017
append using `toappend'
tempfile toappend
save `toappend', replace

use `temp1', clear

merge 1:1 year id_unique_cand using `toappend'
assert _merge!=2
drop _merge

tempfile temp1
save `temp1', replace
}

foreach y in "perso" "vpp"{
	rename sh_`y' sh_`y'_indiv
	replace sh_`y'_indiv = sh_`y'_indiv*100
}

label var sh_perso_indiv "share of personal pronouns"
label var sh_vpp_indiv "share of past participles"

label var orientation "Political orientation"
label var realparty_cand "Affiliated with a party"
label var party "Party name"

save "$intermediate/initial_database", replace

**********************************************************
*** 1/ ASSIGNMENT: TREATMENT AND RUNNING VARIABLE ********
**********************************************************

******* 1A/ Margin of victory at any round ****************

use "$intermediate/initial_database", clear

// Keep candidates ranked 1st and 2nd, at 1st round if no runoff and at second round if runoff
keep if (secround_district==0 & (ranking_cand_R1==1 | ranking_cand_R1==2)) | (secround_district==1 & (ranking_cand_R2==1 | ranking_cand_R2==2)) 
byso id_unique: drop if _N==1 //excludes districts with only 1 candidate at winning stage (first or second round)

// Generate and keep relevant variables
gen ranking=ranking_cand_R1 if secround_district==0
replace ranking=ranking_cand_R2 if secround_district==1
label var ranking "candidate rank at winning stage"

// Running variable for first ranked candidate
byso id_unique (ranking): gen running=prop_voters_votes_cand_R1-prop_voters_votes_cand_R1[_n+1] if secround_cand==0 //diff in vote shares between 1st and 2nd for the 1st round winners
byso id_unique (ranking): replace running=prop_voters_votes_cand_R2-prop_voters_votes_cand_R2[_n+1] if secround_cand==1 //diff in vote shares between 1st and 2nd for the 2nd round winners

// Running variable for second ranked candidate
byso id_unique (ranking): replace running=-running[_n-1] if _n==2 //minus the diff in vote shares between 1st and 2nd for the 2nd
count if (win_cand==0 & running>0) | (win_cand==1 & running<0)
assert r(N)==0
label var running "running variable"

// Exclude ex-aequo
count if running==0 
local exaequo=r(N)/2
drop if running==0

keep id_unique* election_type id_district year id_time nomerge* running orientation party coalition *_cand

byso id_unique: gen temp=orientation==orientation[_n-1] if _n==_N
byso id_unique: egen flag_same_orient=min(temp)
drop temp
label var flag_same_orient "Flag for orientation-level analysis: 2 candidates from same orientation"

byso id_unique: gen temp=party==party[_n-1] if _n==_N
byso id_unique: egen flag_same_party=min(temp) 
drop temp
label var flag_same_party "Flag for party-level analysis: 2 candidates from same party"

byso id_unique: gen temp=coalition==coalition[_n-1] if _n==_N
byso id_unique: egen flag_same_coalition=min(temp) 
drop temp
label var flag_same_coal "Flag for coalition-level analysis: 2 candidates from same coalition"

save "$temp/assignment_win", replace

******* 1B/ Margin of qualification for second round ****************

***** Qualification of third candidate ****
//NB: we use the 12.5% threshold only to avoid the qualification of more than 3 candidates
use "$intermediate/initial_database", clear

// Sample restriction 
keep if secround_type==1 //districts where the second passes the threshold
keep if secround_district==1 & nb_cands_R1>=3 //keeps districts with a runoff and 3 candidates or more in the first round

byso id_unique (ranking_cand_R1): gen temp=prop_voters_votes_cand_R1==prop_voters_votes_cand_R1[_n+1] if ranking_cand_R1==2
ta temp //few cases of equal vote shares between 2nd and 3rd
byso id_unique: egen temp2=min(temp)
drop if temp2==1
drop temp*

byso id_unique (ranking_cand_R1): gen temp=prop_voters_votes_cand_R1==prop_voters_votes_cand_R1[_n+1] if ranking_cand_R1==3
ta temp //few cases of equal vote shares between 3rd and 4th
byso id_unique: egen temp2=min(temp)
drop if temp2==1
drop temp*

keep if ranking_cand_R1==3 //keeps third candidate only

// Runing variable
gen running=prop_registered_votes_cand_R1-0.125 if prop_registered_votes_cand_R1!=. & ((election_type==1 & year>=1978) | (election_type==2 & year>=2011))
replace  running=prop_registered_votes_cand_R1-0.1 if prop_registered_votes_cand_R1!=. & ((election_type==1 & year<1978 & year>=1967) | (election_type==2 & year<2011)) 

gen temp=prop_voters_votes_cand_R1-0.05 if prop_voters_votes_cand_R1!=. & (election_type==1 & year<1967) //distance to threhsold in share of voters
replace running=temp*prop_registered_candvotes_R1 if prop_voters_votes_cand_R1!=. & (election_type==1 & year<1967)  //translates into share of registered voters
drop temp
count if (running>=0 & qualif_cand==0) | (running<0 & qualif_cand==1) 
assert r(N)==0 
label var running "running variable"

// Check problems
ta flag_secround_cand //5 issues that will be dropped from analysis

gen assignment_type=1 //qualification of second candidate vs third

keep id_unique election_type id_district year id_time nomerge* running orientation coalition party *_cand assignment_type
count if nomerge_next==0

save "$temp/assignment_runoff1", replace

******  Qualification of second vs third *****
use "$intermediate/initial_database", clear

// Sample restriction 
keep if secround_type==2 //districts where the second does not pass the threshold
keep if secround_district==1 & nb_cands_R1>=3 //keeps districts with a runoff and 3 candidates or more in the first round

byso id_unique (ranking_cand_R1): gen temp=prop_voters_votes_cand_R1==prop_voters_votes_cand_R1[_n+1] if ranking_cand_R1==2
ta temp //5 cases of equal vote shares between 2nd and 3rd
byso id_unique: egen temp2=min(temp)
drop if temp2==1 //drops districts where 2nd and 3rd have the same vote share
drop temp*

byso id_unique (ranking_cand_R1): gen temp=prop_voters_votes_cand_R1==prop_voters_votes_cand_R1[_n+1] if ranking_cand_R1==3
ta temp //5 cases of equal vote shares between 3rd and 4th
byso id_unique: egen temp2=min(temp)
drop if temp2==1
drop temp*

keep if ranking_cand_R1==2 | ranking_cand_R1==3 //keeps only second and third candidates

// Running variable
byso id_unique (ranking_cand_R1): gen running=prop_registered_votes_cand_R1-prop_registered_votes_cand_R1[_n+1] if ranking_cand_R1==2 //difference in vote shares between second un third
byso id_unique (ranking_cand_R1): replace running=prop_registered_votes_cand_R1-prop_registered_votes_cand_R1[_n-1] if ranking_cand_R1==3 //difference in vote shares between third and second
label var running "running variable"

// Check problems
ta flag_secround_cand
li election_type year prop_registered_votes_cand_R1 ranking_cand_R1 secround_cand if id_unique==2976092011
//1 case of both second and third candidate present in the runoff when the third should not
//but it is in a local election in 2011 which we will exclude from the analaysis as there is not t+1 observation for any district (redistricting) so not a problem for us

gen assignment_type=2 //qualification of second candidate vs third

keep id_unique* election_type id_district year id_time nomerge* running orientation party coalition *_cand assignment_type
count if nomerge_next==0

byso id_unique: gen temp=orientation==orientation[_n-1] if _n==_N
byso id_unique: egen flag_same_orient=min(temp)
drop temp
label var flag_same_orient "Flag for orientation-level analysis: 2 candidates from same orientation"

byso id_unique: gen temp=party==party[_n-1] if _n==_N
byso id_unique: egen flag_same_party=min(temp)
drop temp
label var flag_same_party "Flag for party-level analysis: 2 candidates from same party"

byso id_unique: gen temp=coalition==coalition[_n-1] if _n==_N
byso id_unique: egen flag_same_coalition=min(temp) 
drop temp
label var flag_same_coal "Flag for coalition-level analysis: 2 candidates from same coalition"

save "$temp/assignment_runoff2", replace

***** Pooled strategy ****

use "$temp/assignment_runoff1", clear
append using "$temp/assignment_runoff2"

replace flag_same_orient=0 if flag_same_orient==. //no problem of two orientations identical for the 3rd candidate qualification
replace flag_same_party=0 if flag_same_party==.
replace flag_same_coalition=0 if flag_same_coalition==.

replace assignment_type=3 //qualification of a third candidate or second candidate when below threshold

save "$temp/assignment_runoff3", replace

append using "$temp/assignment_runoff1"
append using "$temp/assignment_runoff2"

label var assignment_type "identification strategy for impact of runoff"

save "$temp/assignment_runoff", replace

**********************************************************
*** 2/ DESCRIPTIVE VARIABLES AT DISTRICT LEVEL ***********
**********************************************************

use "$intermediate/initial_database", clear

// Indicator for districts where a candidate has an unknown orientation or party
count if orientation==""
gen temp=orientation==""
byso id_unique: egen flag_missing_orient=max(temp)
drop temp*
label var flag_missing_orient "Flag for orientation-level analysis (missing orientation for at least one candidate)"

count if political_label==""
gen temp=political_label==""
byso id_unique: egen flag_missing_party=max(temp)
drop temp*
label var flag_missing_party "Flag for party-level analysis (missing label for at least one candidate)"

// Orientation and party of first and second running in second round (to keep track of orientation already qualified when estimating the impact of qualifying for the runoff)
gen orientation_rank1=orientation if ranking_cand_R1==1 & secround_cand==1
label var orientation_rank1 "orientation of the first candidate, if runs in second round"
gen party_rank1=party if ranking_cand_R1==1  & secround_cand==1
label var party_rank1 "party of the first candidate, if runs in second round"
gen orientation_rank2=orientation if ranking_cand_R1==2 & secround_cand==1
label var orientation_rank2 "orientation of the second candidate, if runs in second round"
gen party_rank2=party if ranking_cand_R1==2  & secround_cand==1
label var party_rank2 "party of the second candidate, if runs in second round"

foreach x in "orientation_rank1" "orientation_rank2" "party_rank1" "party_rank2"{
byso id_unique (`x'): replace `x'=`x'[_N]
}

// Election margin
forval i=1/2{
forval j=1/2{
gen temp=prop_voters_votes_cand_R`i' if ranking_cand_R`i'==`j'
byso id_unique: egen temp2=min(temp)
gen votesh_indiv_R`i'_rank`j'=temp2
label var votesh_indiv_R`i'_rank`j' "vote share at round `i', rank `j'"
drop temp*
}
}
gen margin=votesh_indiv_R1_rank1-votesh_indiv_R1_rank2 if secround_district==0
replace margin=votesh_indiv_R2_rank1-votesh_indiv_R2_rank2 if secround_district==1
sum margin
label var margin "margin of victory"

// Number of parties per district
byso id_unique party: gen temp=_n
gen temp2=party!="" & temp==1
byso id_unique: egen nb_unique_party=total(temp2)
drop temp*

label var nb_unique_party "# parties represented in the district"

byso id_time election_type id_district: keep if _n==1 //1 obs per district*year

gen turnout_R1=nb_turnout_R1/nb_registered_R1
label var turnout_R1 "turnout, round 1"
gen turnout_R2=nb_turnout_R2/nb_registered_R2
label var turnout_R2 "turnout, round 2"

local var year departement_code nb_cands* secround_district secround_type nb_registered* turnout* nb_abstention* nb_turnout* nb_blanknull* nb_candvotes* orientation_rank* party_rank* flag_missing_* votesh* margin nb_unique_party

// Dataset at time t
keep id_time election_type id_district `var'

save "$temp/district", replace

// Dataset at time t+1
use "$temp/district", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_next
local y: di subinstr("`z'", "`z'", "next "+"`z'", 1)
label var `x'_next "`y'"
}

byso election_type id_district (id_time): drop if _n==1 //drops first period for each district
replace id_time=id_time-1

save "$temp/district_next", replace

// Dataset at time t-1
use "$temp/district", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev
local y: di subinstr("`z'", "`z'", "previous "+"`z'", 1)
label var `x'_prev "`y'"
}

byso election_type id_district (id_time): drop if _n==_N //drops last period for each district
replace id_time=id_time+1

save "$temp/district_prev", replace

// Dataset at time t-2
use "$temp/district", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev2
local y: di subinstr("`z'", "`z'", "pre-previous "+"`z'", 1)
label var `x'_prev2 "`y'"
}

byso election_type id_district (id_time): drop if _n>=_N-1 //drops last two periods for each district
replace id_time=id_time+2

save "$temp/district_prev2", replace

************************************************
*** 3/ OUTCOMES AT ORIENTATION LEVEL ***********
************************************************

use "$intermediate/initial_database", clear

keep if orientation!="" //keeps only candidates with non missing label

// Generate orientation-level number of candidates, votes, vote share and dummy for winning
// NB: we generate outcomes for the 'nc' (non-classified) orientation as well at this stage but we will exclude those from the analysis as this is not really an orientation
byso id_unique orientation: gen nb_cands_orient_R1=_N 
gen running_orient=nb_cands_orient_R1>0 if nb_cands_orient_R1!=.
byso id_unique orientation: egen nb_rerun_orient=total(rerun_cand)
byso id_unique orientation: egen nb_rerun2_orient=total(rerun2_cand)
byso id_unique orientation: egen nb_win2_orient=total(win2_cand)
byso id_unique orientation: egen nb_party_orient=total(realparty_cand==1)

byso id_unique orientation: egen votes_orient_R1=total(nb_votes_cand_R1)
byso id_unique orientation: egen votesh_orient_R1=total(prop_voters_votes_cand_R1)

gen temp=(prop_voters_votes_cand_R1/votesh_orient_R1)^2
byso id_unique orientation: egen concen_orient_R1=total(temp) //vote share concentration
drop temp
byso id_unique orientation: egen high_votesh_orient_R1=max(prop_voters_votes_cand_R1)

byso id_unique orientation: egen win_orient=max(win_cand)

byso id_unique orientation: egen nb_cands_orient_R2=total(secround_cand==1) //nb of candidates present in the runoff
byso id_unique orientation: egen nb_qualif_orient=total(qualif_cand==1) if qualif_cand!=. //nb of candidates qualified for the runoff
gen secround_orient=nb_cands_orient_R2!=0 //at least one candidate present in the runoff
gen qualif_orient=nb_qualif_orient!=0 if nb_qualif_orient!=. //at least one candidate qualified for runoff

byso id_unique orientation: egen votes_orient_R2=total(nb_votes_cand_R2) if secround_orient==1 //missing value if orientation not present in the second round
byso id_unique orientation: egen votesh_orient_R2=total(prop_voters_votes_cand_R2) if secround_orient==1

gen temp=(prop_voters_votes_cand_R2/votesh_orient_R2)^2
byso id_unique orientation: egen concen_orient_R2=total(temp) //vote share concentration
drop temp
byso id_unique orientation: egen high_votesh_orient_R2=max(prop_voters_votes_cand_R1) //highest vote share among candidates of the orientation

byso id_unique orientation: egen temp=max(win_cand)
gen winR1_orient=(temp==1 & secround_district==0) if nb_cands_orient_R1!=0 //winning and not having a second round, conditional on running at first round
drop temp
byso id_unique orientation: egen winR2_orient=max(win_cand) if nb_cands_orient_R2!=0 & secround_district==1 //winning, conditional on having a second round and running in second round

byso id_unique orientation: egen totexp_orient =  total(totexp_indiv), missing //total expenditures of candidates of the same orientation
byso id_unique orientation: egen totcontrib_orient =  total(totcontrib_indiv), missing  //total contributions received by candidates of the same orientation
byso id_unique orientation: egen donations_orient =  total(donations_indiv), missing  //total donations received by candidates of the same orientation
byso id_unique orientation: egen pecontrib_orient =  total(pecontrib_indiv), missing  //total personal contributions of candidates of the same orientation
byso id_unique orientation: egen pacontrib_orient =  total(pacontrib_indiv), missing  //total contributions received from the candidates' parties - in the same orientation

byso id_unique orientation: egen originality_orient=mean(originality_indiv) //mean originality from same party's candidates

forval i=1/2{
label var nb_cands_orient_R`i' "# candidates of same orientation, round `i'"
label var votes_orient_R`i' "# votes received by the same orientation, round `i'"
label var votesh_orient_R`i' "% voters who voted for same orientation, round `i'"
}

label var running_orient "same orientation running"
label var nb_rerun_orient "# candidates of same orientation who ran in the past"
label var nb_rerun2_orient "# candidates of same orientation who ran more than once"
label var nb_win2_orient "# candidates of same orientation who won in the past"
label var nb_party_orient "# candidates of same orientation who are affiliated with a party"

label var win_orient "election won by same orientation"
label var winR1_orient "election won by same orientation in R1"
label var winR2_orient "election won by same orientation in R2"
label var secround_orient "presence of same orientation at 2nd round"
label var qualif_orient "qualification of same orientation for 2nd round"
label var nb_qualif_orient "# candidates of same orientation qualified for 2nd round"

label var totexp_orient "total expenditures of candidates of the same orientation, both rounds"
label var totcontrib_orient "total contributions received by candidates of the same orientation, both rounds"
label var donations_orient "total donations received by candidates of the same orientation, both rounds"
label var pecontrib_orient "total personal contributions of candidates of the same orientation, both rounds"
label var pacontrib_orient "total contributions received from the candidates' parties in the same orientation, both rounds"

label var originality_orient "mean originality of candidates from same orientation"

label var concen_orient_R1 "vote share concentration within orientation at 1st round"
label var concen_orient_R2 "vote share concentration within orientation at 2nd round"
label var high_votesh_orient_R1 "highest vote share within orientation at 1st round"
label var high_votesh_orient_R2 "highest vote share within orientation at 2nd round"

// Outcomes for each orientation
local var nb_cands_orient_R1 running_orient nb_cands_orient_R2 nb_rerun_orient nb_rerun2_orient nb_win2_orient nb_party_orient votes_orient_R1 votes_orient_R2 votesh_orient_R1 votesh_orient_R2 win_orient winR1_orient winR2_orient secround_orient qualif_orient nb_qualif_orient ///
totexp_orient totcontrib_orient donations_orient pecontrib_orient pacontrib_orient
foreach y in "fr" "r" "c" "l" "fl" "nc"{
foreach x of varlist `var'{
gen temp=`x' if orientation=="`y'"
byso id_unique: egen `x'_`y'=min(temp)
drop temp
local z: variable label `x'
local w:  di subinstr("`z'", "same orientation", "`y'", 1)
label var `x'_`y' "`w'"
}
}

local var nb_cands_orient_R1 running_orient nb_cands_orient_R2 nb_rerun_orient nb_rerun2_orient nb_win2_orient nb_party_orient votes_orient_R1 votes_orient_R2 votesh_orient_R1 votesh_orient_R2 win_orient secround_orient qualif_orient nb_qualif_orient ///
totexp_orient totcontrib_orient donations_orient pecontrib_orient pacontrib_orient
foreach y in "fr" "r" "c" "l" "fl" "nc"{
foreach x of varlist `var'{
replace `x'_`y'=0 if `x'_`y'==.
}
}

byso id_unique orientation: keep if _n==1 //1 obs per district*year*orientation

// Dataset at time t
local var *_orient*
keep id_time election_type id_district orientation `var'

save "$temp/orient", replace

// Dataset at time t+1
use "$temp/orient", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_next
local y: di subinstr("`z'", "`z'", "next "+"`z'", 1)
label var `x'_next "`y'"
}

byso election_type id_district orientation (id_time): drop if _n==1 
replace id_time=id_time-1

save "$temp/orient_next", replace

// Dataset at time t-1
use "$temp/orient", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev
local y: di subinstr("`z'", "`z'", "previous "+"`z'", 1)
label var `x'_prev "`y'"
}

byso election_type id_district orientation (id_time): drop if _n==_N
replace id_time=id_time+1

save "$temp/orient_prev", replace

// Dataset at time t-2
use "$temp/orient", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev2
local y: di subinstr("`z'", "`z'", "pre-previous "+"`z'", 1)
label var `x'_prev2 "`y'"
}

byso election_type id_district orientation (id_time): drop if _n>=_N-1 //drops last two periods for each district
replace id_time=id_time+2

save "$temp/orient_prev2", replace

******************************************
*** 4/ OUTCOMES AT PARTY LEVEL ***********
******************************************

use "$intermediate/initial_database", clear

keep if party!="" //keeps only candidates affiliated with a party lineage

// Generate party-level number of candidates, votes, vote share and dummy for winning
byso id_unique party: gen nb_cands_party_R1=_N
gen running_party=nb_cands_party_R1>0 if nb_cands_party_R1!=.
byso id_unique party: egen nb_rerun_party=total(rerun_cand)
byso id_unique party: egen nb_rerun2_party=total(rerun2_cand)
byso id_unique party: egen nb_win2_party=total(win2_cand)
byso id_unique party: egen votes_party_R1=total(nb_votes_cand_R1)
byso id_unique party: egen votesh_party_R1=total(prop_voters_votes_cand_R1)
byso id_unique party: egen win_party=max(win_cand)

byso id_unique party: egen nb_cands_party_R2=total(secround_cand==1) //nb of candidates present in the runoff
byso id_unique party: egen nb_qualif_party=total(qualif_cand==1) if qualif_cand!=. //nb of candidates qualified for the runoff
gen secround_party=nb_cands_party_R2!=0 //at least one candidate present in the runoff
gen qualif_party=nb_qualif_party!=0 if nb_qualif_party!=. //at least one candidate qualified for runoff

byso id_unique party: egen votes_party_R2=total(nb_votes_cand_R2) if secround_party==1 //missing value if party not present in the second round
byso id_unique party: egen votesh_party_R2=total(prop_voters_votes_cand_R2) if secround_party==1

byso id_unique party: egen temp=max(win_cand)
gen winR1_party=(temp==1 & secround_district==0) if nb_cands_party_R1!=0 //winning and not having a second round, conditional on running at first round
drop temp
byso id_unique party: egen winR2_party=max(win_cand) if nb_cands_party_R2!=0 & secround_district==1 //winning, conditional on having a second round and running in second round

byso id_unique party: egen totexp_party =  total(totexp_indiv), missing //total expenditures of candidates in the same party
byso id_unique party: egen totcontrib_party =  total(totcontrib_indiv), missing  //total contributions received by candidates in the same party
byso id_unique party: egen donations_party =  total(donations_indiv), missing  //total donations received by candidates in the same party
byso id_unique party: egen pecontrib_party =  total(pecontrib_indiv), missing  //total personal contributions of candidates in the same party
byso id_unique party: egen pacontrib_party =  total(pacontrib_indiv), missing  //total contributions from the candidates'party 

byso id_unique party: egen originality_party=mean(originality_indiv) //mean originality from same party's candidates

forval i=1/2{
label var nb_cands_party_R`i' "# candidates of same party, round `i'"
label var votes_party_R`i' "# votes received by the same party, round `i'"
label var votesh_party_R`i' "% voters who voted for same party, round `i'"
}

label var running_party "same party running"
label var nb_rerun_party "# candidates of same party who ran in the past"
label var nb_rerun2_party "# candidates of same party who ran more than once"
label var nb_win2_party "# candidates of same party who won in the past"
label var win_party "election won by same party"
label var secround_party "presence of same party at 2nd round"
label var qualif_party "qualification of same party for 2nd round"
label var nb_qualif_party "# candidates of same party qualified for 2nd round"

label var winR1_party "election won by same party in R1"
label var winR2_party "election won by same party in R2"

label var totexp_party "total expenditures of candidates in the same party, both rounds"
label var totcontrib_party "total contributions received by candidates in the same party, both rounds"
label var donations_party "total donations received by candidates in the same party, both rounds"
label var pecontrib_party "total personal contributions of candidates of the same party, both rounds"
label var pacontrib_party "total contributions from the candidates'party, both rounds"

label var originality_party "mean originality of candidates from same party"

// Do the same for contiguous-party-level outcomes (linked over time through historical parties)
gen cont1_FDG=party=="SOC" | party=="RG" | party=="VEC"
gen cont1_RG=party=="SOC" | party=="FDG" | party=="VEC"
gen cont1_VEC=party=="SOC" | party=="RG" | party=="FDG"
gen cont1_SOC=party=="FDG" | party=="RG" | party=="VEC"
gen cont1_UMP=party=="MDM"
gen cont1_MDM=party=="UMP"
gen cont1_FN=0 //no historical alliance for the FN

gen cont2_FDG=party=="SOC" | party=="RG" | party=="VEC"
gen cont2_VEC=party=="SOC" | party=="FDG" | party=="RG" | party=="MDM"
gen cont2_RG=party=="SOC" | party=="FDG" | party=="VEC" | party=="MDM"
gen cont2_SOC=party=="VEC" | party=="FDG" | party=="RG" | party=="MDM"
gen cont2_MDM=party=="SOC" | party=="VEC" | party=="RG" | party=="UMP" 
gen cont2_UMP=party=="MDM" | party=="FN"
gen cont2_FN=party=="UMP"

gen cont3_FDG=party=="MDM" | party=="UMP" | party=="FN"
gen cont3_VEC=party=="UMP" | party=="FN"
gen cont3_RG=party=="UMP" | party=="FN"
gen cont3_SOC=party=="UMP" | party=="FN"
gen cont3_MDM=party=="FG" | party=="FN" 
gen cont3_UMP=party=="FG" | party=="SOC" | party=="VEC" | party=="RG"
gen cont3_FN=party=="FG" | party=="SOC" | party=="VEC" | party=="RG" | party=="MDM"

tempfile temp
save `temp', replace

forval i=1/3{
foreach y in "FDG" "MDM" "RG" "SOC" "UMP" "VEC"{
use `temp', clear
keep if cont`i'_`y'==1

byso id_unique: gen nb_cands_cont`i'_R1=_N
gen running_cont`i'=nb_cands_cont`i'_R1>0 if nb_cands_cont`i'_R1!=.
byso id_unique: egen nb_rerun_cont`i'=total(rerun_cand)
byso id_unique: egen nb_rerun2_cont`i'=total(rerun2_cand)
byso id_unique: egen nb_win2_cont`i'=total(win2_cand)

byso id_unique: egen votes_cont`i'_R1=total(nb_votes_cand_R1)
byso id_unique: egen votesh_cont`i'_R1=total(prop_voters_votes_cand_R1)
byso id_unique: egen win_cont`i'=max(win_cand)

byso id_unique: egen nb_cands_cont`i'_R2=total(secround_cand==1) //nb of candidates actually present in the runoff, from the second round coalition agreement made beforehand
byso id_unique: egen nb_qualif_cont`i'=total(qualif_cand==1) if qualif_cand!=. //nb of candidates qualified for the runoff, from the first round coalition (before deciding to stay or not)
gen secround_cont`i'=nb_cands_cont`i'_R2!=0 //at least one candidate present in the runoff
gen qualif_cont`i'=nb_qualif_cont`i'!=0 if nb_qualif_cont`i'!=. //at least one candidate qualified for runoff

byso id_unique: egen votes_cont`i'_R2=total(nb_votes_cand_R2) if secround_cont`i'==1 //missing value if coalition not present in the second round
byso id_unique: egen votesh_cont`i'_R2=total(prop_voters_votes_cand_R2) if secround_cont`i'==1

byso id_unique: egen temp=max(win_cand)
gen winR1_cont`i'=(temp==1 & secround_district==0) if nb_cands_cont`i'_R1!=0 //winning and not having a second round, conditional on running at first round
drop temp
byso id_unique: egen winR2_cont`i'=max(win_cand) if nb_cands_cont`i'_R2!=0 & secround_district==1 //winning, conditional on having a second round and running in second round

byso id_unique: keep if _n==1
keep id_unique *_cont`i'*
gen party="`y'"

tempfile temp_`y'_`i'
save `temp_`y'_`i'', replace
}
}

//for FN, 2nd and 3rd definition only
forval i=2/3{
foreach y in "FN"{
use `temp', clear
keep if cont`i'_`y'==1

byso id_unique: gen nb_cands_cont`i'_R1=_N
gen running_cont`i'=nb_cands_cont`i'_R1>0 if nb_cands_cont`i'_R1!=.
byso id_unique: egen nb_rerun_cont`i'=total(rerun_cand)
byso id_unique: egen nb_rerun2_cont`i'=total(rerun2_cand)
byso id_unique: egen nb_win2_cont`i'=total(win2_cand)

byso id_unique: egen votes_cont`i'_R1=total(nb_votes_cand_R1)
byso id_unique: egen votesh_cont`i'_R1=total(prop_voters_votes_cand_R1)
byso id_unique: egen win_cont`i'=max(win_cand)

byso id_unique: egen nb_cands_cont`i'_R2=total(secround_cand==1) //nb of candidates actually present in the runoff, from the second round coalition agreement made beforehand
byso id_unique: egen nb_qualif_cont`i'=total(qualif_cand==1) if qualif_cand!=. //nb of candidates qualified for the runoff, from the first round coalition (before deciding to stay or not)
gen secround_cont`i'=nb_cands_cont`i'_R2!=0 //at least one candidate present in the runoff
gen qualif_cont`i'=nb_qualif_cont`i'!=0 if nb_qualif_cont`i'!=. //at least one candidate qualified for runoff

byso id_unique: egen votes_cont`i'_R2=total(nb_votes_cand_R2) if secround_cont`i'==1 //missing value if coalition not present in the second round
byso id_unique: egen votesh_cont`i'_R2=total(prop_voters_votes_cand_R2) if secround_cont`i'==1

byso id_unique: egen temp=max(win_cand)
gen winR1_cont`i'=(temp==1 & secround_district==0) if nb_cands_cont`i'_R1!=0 //winning and not having a second round, conditional on running at first round
drop temp
byso id_unique: egen winR2_cont`i'=max(win_cand) if nb_cands_cont`i'_R2!=0 & secround_district==1 //winning, conditional on having a second round and running in second round

byso id_unique: keep if _n==1
keep id_unique *_cont`i'*
gen party="`y'"

tempfile temp_`y'_`i'
save `temp_`y'_`i'', replace
}
}

use `temp', clear
forval i=1/3{
foreach y in "FDG" "MDM" "RG" "SOC" "UMP" "VEC"{
merge m:1 id_unique party using `temp_`y'_`i'', update
drop if _merge==2
drop _merge
}
}
forval i=2/3{
foreach y in "FN"{
merge m:1 id_unique party using `temp_`y'_`i'', update
drop if _merge==2
drop _merge
}
}


forval i=1/3{
forval j=1/2{
label var nb_cands_cont`i'_R`j' "# candidates of contiguous parties `i', round `i'"
label var votes_cont`i'_R`j' "# votes received by contiguous parties `i', round `i'"
label var votesh_cont`i'_R`j' "% voters who voted for contiguous parties `i', round `i'"
}

label var running_cont`i' "contiguous parties running"
label var nb_rerun_cont`i' "# candidates of contiguous parties `i' who ran in the past"
label var nb_rerun2_cont`i' "# candidates of contiguous parties `i' who ran more than once"
label var nb_win2_cont`i' "# candidates of contiguous parties `i' who won in the past"

label var win_cont`i' "election won by contiguous parties `i'"
label var secround_cont`i' "presence of contiguous parties `i' at 2nd round"
label var qualif_cont`i' "qualification of contiguous parties `i' for 2nd round"
label var nb_qualif_cont`i' "# candidates of contiguous parties `i' qualified for 2nd round"

label var winR1_cont`i' "election won by contiguous parties `i' in R1"
label var winR2_cont`i' "election won by contiguous parties `i' in R2"
}

// Outcomes for each party
local var nb_cands_party_R1 running_party nb_cands_party_R2 nb_rerun_party nb_rerun2_party nb_win2_party votes_party_R1 votes_party_R2 votesh_party_R1 votesh_party_R2 win_party winR1_party winR2_party secround_party qualif_party nb_qualif_party ///
totexp_party totcontrib_party donations_party pecontrib_party pacontrib_party 
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
foreach x of varlist `var'{
gen temp=`x' if party=="`y'"
byso id_unique: egen `x'_`y'=min(temp)
drop temp
local z: variable label `x'
local w:  di subinstr("`z'", "same party", "`y'", 1)
label var `x'_`y' "`w'"
}
}

local var nb_cands_party_R1 running_party nb_cands_party_R2 nb_rerun_party nb_rerun2_party nb_win2_party votes_party_R1 votes_party_R2 votesh_party_R1 votesh_party_R2 win_party winR1_party winR2_party secround_party qualif_party nb_qualif_party ///
totexp_party totcontrib_party donations_party pecontrib_party pacontrib_party 
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
foreach x of varlist `var'{
replace `x'_`y'=0 if `x'_`y'==.
}
}

// Flag for parties not represented at all in an election year
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
byso year: egen temp=total(nb_cands_party_R1_`y')
ta temp
gen flag_no_party_`y'=temp==0
drop temp
label var flag_no_party_`y' "election year without any candidate from `y'"
}

byso id_unique party: keep if _n==1 //1 obs per district*year*party

// Dataset at time t
local var *_party* *_cont1* *_cont2* *_cont3*
keep id_time election_type id_district party `var'

save "$temp/party", replace

// Dataset at time t+1
use "$temp/party", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_next
local y: di subinstr("`z'", "`z'", "next "+"`z'", 1)
label var `x'_next "`y'"
}

byso election_type id_district party (id_time): drop if _n==1 
replace id_time=id_time-1

save "$temp/party_next", replace

// Dataset at time t-1
use "$temp/party", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev
local y: di subinstr("`z'", "`z'", "previous "+"`z'", 1)
label var `x'_prev "`y'"
}

byso election_type id_district party (id_time): drop if _n==_N
replace id_time=id_time+1

save "$temp/party_prev", replace

// Dataset at time t-2
use "$temp/party", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev2
local y: di subinstr("`z'", "`z'", "pre-previous "+"`z'", 1)
label var `x'_prev2 "`y'"
}

byso election_type id_district party (id_time): drop if _n>=_N-1 //drops last two periods for each district
replace id_time=id_time+2

save "$temp/party_prev2", replace

******************************************
*** 5/ OUTCOMES AT COALITION LEVEL *******
******************************************

use "$intermediate/initial_database", clear

keep if coalition!="" //keeps only candidates affiliated with a coalition lineage

byso id_unique coalition: gen nb_cands_coal_R1=_N
gen running_coal=nb_cands_coal_R1>0 if nb_cands_coal_R1!=.
byso id_unique coalition: egen nb_rerun_coal=total(rerun_cand)
byso id_unique coalition: egen nb_rerun2_coal=total(rerun2_cand)
byso id_unique coalition: egen nb_win2_coal=total(win2_cand)
byso id_unique coalition: egen votes_coal_R1=total(nb_votes_cand_R1)
byso id_unique coalition: egen votesh_coal_R1=total(prop_voters_votes_cand_R1)
byso id_unique coalition: egen win_coal=max(win_cand)

byso id_unique coalition: egen nb_cands_coal_R2=total(secround_cand==1) //nb of candidates actually present in the runoff, from the second round coalition agreement made beforehand
byso id_unique coalition: egen nb_qualif_coal=total(qualif_cand==1) if qualif_cand!=. //nb of candidates qualified for the runoff, from the first round coalition (before deciding to stay or not)
gen secround_coal=nb_cands_coal_R2!=0 //at least one candidate present in the runoff
gen qualif_coal=nb_qualif_coal!=0 if nb_qualif_coal!=. //at least one candidate qualified for runoff

byso id_unique coalition: egen votes_coal_R2=total(nb_votes_cand_R2) if secround_coal==1 //missing value if coalition not present in the second round
byso id_unique coalition: egen votesh_coal_R2=total(prop_voters_votes_cand_R2) if secround_coal==1

byso id_unique coalition: egen temp=max(win_cand)
gen winR1_coal=(temp==1 & secround_district==0) if nb_cands_coal_R1!=0 //winning and not having a second round, conditional on running at first round
drop temp
byso id_unique coalition: egen winR2_coal=max(win_cand) if nb_cands_coal_R2!=0 & secround_district==1 //winning, conditional on having a second round and running in second round

forval i=1/2{
label var nb_cands_coal_R`i' "# candidates of same coalition, round `i'"
label var votes_coal_R`i' "# votes received by the same coalition, round `i'"
label var votesh_coal_R`i' "% voters who voted for same coalition, round `i'"
}

label var running_coal "coalition running"
label var nb_rerun_coal "# candidates of same coalition who ran in the past"
label var nb_rerun2_coal "# candidates of same coalition who ran more than once"
label var nb_win2_coal "# candidates of same coalition who won in the past"
label var win_coal "election won by same coalition"
label var secround_coal "presence of same coalition at 2nd round"
label var qualif_coal "qualification of same coalition for 2nd round"
label var nb_qualif_coal "# candidates of same coalition qualified for 2nd round"

label var winR1_coal "election won by same coalition in R1"
label var winR2_coal "election won by same coalition in R2"

// Outcomes for each coalition
local var nb_cands_coal_R1 running_coal nb_cands_coal_R2 nb_rerun_coal nb_rerun2_coal nb_win2_coal votes_coal_R1 votes_coal_R2 votesh_coal_R1 votesh_coal_R2 win_coal winR1_coal winR2_coal secround_coal qualif_coal nb_qualif_coal
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
foreach x of varlist `var'{
gen temp=`x' if coalition=="`y'"
byso id_unique: egen `x'_`y'=min(temp)
drop temp
local z: variable label `x'
local w:  di subinstr("`z'", "same coalition", "`y'", 1)
label var `x'_`y' "`w'"
}
}

local var nb_cands_coal_R1 running_coal nb_cands_coal_R2 nb_rerun_coal nb_rerun2_coal nb_win2_coal votes_coal_R1 votes_coal_R2 votesh_coal_R1 votesh_coal_R2 win_coal winR1_coal winR2_coal secround_coal qualif_coal nb_qualif_coal
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
foreach x of varlist `var'{
replace `x'_`y'=0 if `x'_`y'==.
}
}

// Flag for parties not represented at all in an election year
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
byso year: egen temp=total(nb_cands_coal_R1_`y')
ta temp
gen flag_no_coal_`y'=temp==0
drop temp
label var flag_no_coal_`y' "election year without any candidate from coalition `y'"
}

byso id_unique coalition: keep if _n==1 //1 obs per district*year*coalition

// Dataset at time t
local var *_coal*
keep id_time election_type id_district coalition `var'

save "$temp/coalition", replace

// Dataset at time t+1
use "$temp/coalition", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_next
local y: di subinstr("`z'", "`z'", "next "+"`z'", 1)
label var `x'_next "`y'"
}

byso election_type id_district coalition (id_time): drop if _n==1 
replace id_time=id_time-1

save "$temp/coalition_next", replace

// Dataset at time t-1
use "$temp/coalition", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev
local y: di subinstr("`z'", "`z'", "previous "+"`z'", 1)
label var `x'_prev "`y'"
}

byso election_type id_district coalition (id_time): drop if _n==_N
replace id_time=id_time+1

save "$temp/coalition_prev", replace

// Dataset at time t-2
use "$temp/coalition", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev2
local y: di subinstr("`z'", "`z'", "pre-previous "+"`z'", 1)
label var `x'_prev2 "`y'"
}

byso election_type id_district coalition (id_time): drop if _n>=_N-1 //drops last two periods for each district
replace id_time=id_time+2

save "$temp/coalition_prev2", replace

******************************************
*** 6/ OUTCOMES AT CANDIDATE LEVEL *******
******************************************

use "$intermediate/initial_database", clear

// Rename individual-level outcomes when needed
gen running_indiv=1
gen votes_indiv_R1=nb_votes_cand_R1
gen ranking_indiv_R1=ranking_cand_R1
gen votesh_indiv_R1=prop_voters_votes_cand_R1
gen win_indiv=win_cand

gen secround_indiv=secround_cand==1 
gen qualif_indiv=qualif_cand==1

gen votes_indiv_R2=nb_votes_cand_R2 if secround_indiv==1 //missing value if party not present in the second round
gen votesh_indiv_R2=prop_voters_votes_cand_R2 if secround_indiv==1
gen ranking_indiv_R2=ranking_cand_R2

gen winR1_indiv=(win_cand==1 & secround_district==0) //winning and not having a second round
gen winR2_indiv=win_cand if secround_cand==1 //winning, conditional on having a second round and running in second round

forval i=1/2{
label var votes_indiv_R`i' "# votes received by candidate, round `i'"
label var votesh_indiv_R`i' "% voters who voted for candidate, round `i'"
label var ranking_indiv_R`i' "candidate's ranking, round `i'"
}

label var running_indiv "candidate running"
label var win_indiv "election won by candidate"
label var secround_indiv "presence of candidate at 2nd round"
label var qualif_indiv "qualification of candidate for 2nd round"

label var winR1_indiv "election won by candidate in R1"
label var winR2_indiv "election won by candidate in R2"

// Dataset at time t
local var *_indiv*
keep id_time election_type id_district id_unique_cand* orientation coalition realparty_cand party `var'

save "$temp/indiv", replace

// Dataset at time t+1
use "$temp/indiv", clear

foreach x of varlist `var'{
rename `x' `x'_next
local z: variable label `x'
local y: di subinstr("`z'", "`z'", "next "+"`z'", 1)
label var `x'_next "`y'"
}

keep if id_unique_cand_prev!=.
replace id_unique_cand=id_unique_cand_prev
rename orientation orientation_next
rename party party_next
rename coalition coalition_next
rename realparty_cand realparty_cand_next

save "$temp/indiv_next", replace

// Dataset at time t-1
use "$temp/indiv", clear

foreach x of varlist `var'{
rename `x' `x'_prev
local z: variable label `x'
local y: di subinstr("`z'", "`z'", "previous "+"`z'", 1)
label var `x'_prev "`y'"
}

keep if id_unique_cand_next!=.
replace id_unique_cand=id_unique_cand_next
rename orientation orientation_prev
rename party party_prev
rename coalition coalition_prev
rename realparty_cand realparty_cand_prev

save "$temp/indiv_prev", replace

// Dataset at time t-2
use "$temp/indiv", clear

foreach x of varlist `var'{
local z: variable label `x'
rename `x' `x'_prev2
local y: di subinstr("`z'", "`z'", "pre-previous "+"`z'", 1)
label var `x'_prev2 "`y'"
}

keep if id_unique_cand_next2!=.
replace id_unique_cand=id_unique_cand_next2
rename orientation orientation_prev2
rename party party_prev2
rename coalition coalition_prev2
rename realparty_cand realparty_cand_prev2

save "$temp/indiv_prev2", replace

***********************************************************
*** 7/ MERGE OUTCOMES WITH ASSIGNMENT DATABASES ***********
***********************************************************

***** 7A/ Merge with win treatment assignment *****
use "$temp/assignment_win", clear

// District level
merge m:1 election_type id_district id_time using "$temp/district"
ta nb_cands_R1 nb_cands_R2 if _merge==2 //ok, all districts excluded because one candidate only or because two winners ex-aequo
gen nomerge_temp=_merge==2
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_next"
assert _merge!=2
count if (_merge==1 & nomerge_next!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_next==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev"
assert _merge!=2
count if (_merge==1 & nomerge_prev!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_prev==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev2"
assert _merge!=2
count if (_merge==1 & nomerge_prev2!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_prev2==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

drop if nomerge_temp==1 //districts we have to drop because only one candidate left at winning round (so no treatment assignment)
drop nomerge_temp

// Orientation level
merge m:1 election_type id_district orientation id_time using "$temp/orient"
count if (_merge==1 & orientation!="") | (_merge!=1 & orientation=="")
assert r(N)==0 //candidates without a known political label
drop if _merge==2 //orientations that do no match with either winner or runner up
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_next"
local var nb_cands_orient_R1_next running_orient_next nb_cands_orient_R2_next nb_rerun_orient_next nb_rerun2_orient_next nb_win2_orient_next nb_party_orient_next nb_qualif_orient_next secround_orient_next qualif_orient_next win_orient_next votesh_orient_R1_next votes_orient_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

local var nb_cands_orient_R1_*_next running_orient_*_next win_orient_*_next votesh_orient_R1_*_next nb_party_orient_*_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev"
local var nb_cands_orient_R1_prev running_orient_prev nb_cands_orient_R2_prev nb_rerun_orient_prev nb_rerun2_orient_prev nb_win2_orient_prev nb_party_orient_prev nb_qualif_orient_prev secround_orient_prev qualif_orient_prev win_orient_prev votesh_orient_R1_prev votes_orient_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev2"
local var nb_cands_orient_R1_prev2 running_orient_prev2 nb_cands_orient_R2_prev2 nb_rerun_orient_prev2 nb_rerun2_orient_prev2 nb_win2_orient_prev2 nb_party_orient_prev2 nb_qualif_orient_prev2 secround_orient_prev2 qualif_orient_prev2 win_orient_prev2 votesh_orient_R1_prev2 votes_orient_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Party level
merge m:1 election_type id_district party id_time using "$temp/party"
count if (_merge==1 & party!="") | (_merge!=1 & party=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical party
drop if _merge==2 //parties that do no match with either winner or runner up
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_next"
local var nb_cands_party_R1_next running_party_next nb_cands_party_R2_next nb_rerun_party_next nb_rerun2_party_next nb_win2_party_next nb_qualif_party_next secround_party_next qualif_party_next win_party_next votesh_party_R1_next votes_party_R1_next ///
nb_cands_cont*_R1_next running_cont*_next nb_cands_cont*_R2_next nb_rerun_cont*_next nb_rerun2_cont*_next nb_win2_cont*_next nb_qualif_cont*_next secround_cont*_next qualif_cont*_next win_cont*_next votesh_cont*_R1_next votes_cont*_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

local var nb_cands_party_R1_*_next running_party_*_next win_party_*_next votesh_party_R1_*_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev"
local var nb_cands_party_R1_prev running_party_prev nb_cands_party_R2_prev nb_rerun_party_prev nb_rerun2_party_prev nb_win2_party_prev nb_qualif_party_prev secround_party_prev qualif_party_prev win_party_prev votesh_party_R1_prev votes_party_R1_prev ///
nb_cands_cont*_R1_prev running_cont*_prev nb_cands_cont*_R2_prev nb_rerun_cont*_prev nb_rerun2_cont*_prev nb_win2_cont*_prev nb_qualif_cont*_prev secround_cont*_prev qualif_cont*_prev win_cont*_prev votesh_cont*_R1_prev votes_cont*_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev2"
local var nb_cands_party_R1_prev2 running_party_prev2 nb_cands_party_R2_prev2 nb_rerun_party_prev2 nb_rerun2_party_prev2 nb_win2_party_prev2 nb_qualif_party_prev2 secround_party_prev2 qualif_party_prev2 win_party_prev2 votesh_party_R1_prev2 votes_party_R1_prev2 ///
nb_cands_cont*_R1_prev2 running_cont*_prev2 nb_cands_cont*_R2_prev2 nb_rerun_cont*_prev2 nb_rerun2_cont*_prev2 nb_win2_cont*_prev2 nb_qualif_cont*_prev2 secround_cont*_prev2 qualif_cont*_prev2 win_cont*_prev2 votesh_cont*_R1_prev2 votes_cont*_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Coalition level
merge m:1 election_type id_district coalition id_time using "$temp/coalition"
count if (_merge==1 & coalition!="") | (_merge!=1 & coalition=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical party
drop if _merge==2 //parties that do no match with either winner or runner up
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_next"
local var nb_cands_coal_R1_next running_coal_next nb_cands_coal_R2_next nb_rerun_coal_next nb_rerun2_coal_next nb_win2_coal_next nb_qualif_coal_next secround_coal_next qualif_coal_next win_coal_next votesh_coal_R1_next votes_coal_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev"
local var nb_cands_coal_R1_prev running_coal_prev nb_cands_coal_R2_prev nb_rerun_coal_prev nb_rerun2_coal_prev nb_win2_coal_prev nb_qualif_coal_prev secround_coal_prev qualif_coal_prev win_coal_prev votesh_coal_R1_prev votes_coal_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev2"
local var nb_cands_coal_R1_prev2 running_coal_prev2 nb_cands_coal_R2_prev2 nb_rerun_coal_prev2 nb_rerun2_coal_prev2 nb_win2_coal_prev2 nb_qualif_coal_prev2 secround_coal_prev2 qualif_coal_prev2 win_coal_prev2 votesh_coal_R1_prev2 votes_coal_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Individual level
merge 1:1 id_unique_cand using "$temp/indiv"
assert _merge!=1
drop if _merge==2 //candidates not retained in assignment dataset
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_next"
local var running_indiv_next secround_indiv_next qualif_indiv_next win_indiv_next votesh_indiv_R1_next votes_indiv_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_prev"
local var running_indiv_prev secround_indiv_prev qualif_indiv_prev win_indiv_prev votesh_indiv_R1_prev votes_indiv_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_prev2"
local var running_indiv_prev2 secround_indiv_prev2 qualif_indiv_prev2 win_indiv_prev2 votesh_indiv_R1_prev2 votes_indiv_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Sample definition
gen main_sample_orient=(orientation!="nc" & flag_same_orient==0 & nomerge_next==0 & flag_missing_orient_next==0)
label var main_sample_orient "main sample for orientation-level analysis"
gen placebo_sample_orient=(main_sample_orient==1 & nomerge_prev==0 & flag_missing_orient_prev==0)
label var placebo_sample_orient "placebo sample for orientation-level analysis"
gen placebo2_sample_orient=(main_sample_orient==1 & nomerge_prev2==0 & flag_missing_orient_prev2==0)
label var placebo2_sample_orient "placebo (t-2) sample for orientation-level analysis"

gen main_sample_party=(party!="" & flag_same_party==0 & nomerge_next==0 & flag_missing_party_next==0)
label var main_sample_party "main sample for party-level analysis"
gen placebo_sample_party=(main_sample_party==1 & nomerge_prev==0 & flag_missing_party_prev==0)
label var placebo_sample_party "placebo sample for party-level analysis"
gen placebo2_sample_party=(main_sample_party==1 & nomerge_prev2==0 & flag_missing_party_prev2==0)
label var placebo2_sample_party "placebo (t-2) sample for party-level analysis"

gen main_sample_coal=(coalition!="" & flag_same_coal==0 & nomerge_next==0 & flag_missing_party_next==0) //we use the same missing political_label condition as it is what we base our definition of coalition on
label var main_sample_coal "main sample for coalition-level analysis"
gen placebo_sample_coal=(main_sample_coal==1 & nomerge_prev==0 & flag_missing_party_prev==0)
label var placebo_sample_coal "placebo sample for coalition-level analysis"
gen placebo2_sample_coal=(main_sample_coal==1 & nomerge_prev2==0 & flag_missing_party_prev2==0)
label var placebo2_sample_coal "placebo (t-2) sample for coalition-level analysis"

gen main_sample_indiv=nomerge_next==0
gen placebo_sample_indiv=(main_sample_indiv==1 & nomerge_prev==0)
gen placebo2_sample_indiv=(main_sample_indiv==1 & nomerge_prev2==0)
label var main_sample_indiv "main sample for candidate-level analysis"
label var placebo_sample_indiv "placebo sample for candidate-level analysis"
label var placebo2_sample_indiv "placebo (t-2) sample for candidate-level analysis"

gen flag_noparty_next=0
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
gen temp=flag_no_party_`y'_next==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_noparty_next=1 if temp2==1 & party=="`y'"
drop temp*
}
label var flag_noparty_next "next election without any candidate from the same party"

gen flag_noparty_prev=0
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
gen temp=flag_no_party_`y'_prev==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_noparty_prev=1 if temp2==1 & party=="`y'"
drop temp*
}
label var flag_noparty_prev "previous election without any candidate from the same party"

gen flag_nocoal_next=0
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
gen temp=flag_no_coal_`y'_next==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_nocoal_next=1 if temp2==1 & coalition=="`y'"
drop temp*
}
label var flag_nocoal_next "next election without any candidate from the same coalition"

gen flag_nocoal_prev=0
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
gen temp=flag_no_coal_`y'_prev==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_nocoal_prev=1 if temp2==1 & coalition=="`y'"
drop temp*
}
label var flag_nocoal_prev "previous election without any candidate from the same coalition"

// Add left/right + orientation dummies and incumbent majority vs others (at NEXT election)
foreach o in "fl" "l" "c" "r" "fr" "nc"{
gen dummy_`o'=orientation=="`o'" if orientation!=""
label var dummy_`o' "orientation is `o'"
}
gen left=orientation=="fl" | orientation=="l"  if orientation!=""
label var left "left or far-left"

gen samepres=0 if realparty_cand==1
replace samepres=1 if election_type==1 & year_next==1962 & party=="UMP"
replace samepres=1 if election_type==1 & year_next==1967 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1968 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1973 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1978 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1981 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1988 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1993 & party=="SOC"
replace samepres=1 if election_type==1 & year_next==1997 & (party=="UMP" | party=="UDF")
replace samepres=1 if election_type==1 & year_next==2002 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==1 & year_next==2007 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2012 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2017 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==1982 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1985 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1988 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1992 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1994 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==2 & year_next==1998 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2001 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2004 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2008 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2011 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2015 & (party=="SOC" | party=="RG")
label var samepres "belongs to next incumbent/current national majority"

// Generate number of OTHER candidates from same orientation
gen nb_othercands_orient_R1=nb_cands_orient_R1-1
label var nb_othercands_orient_R1 "# other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_next=nb_cands_orient_R1_next
replace nb_othercands_orient_R1_next=nb_cands_orient_R1_next-1 if orientation==orientation_next & running_indiv_next==1
label var nb_othercands_orient_R1_next "next # other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev
replace nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev-1 if orientation==orientation_prev & running_indiv_prev==1
label var nb_othercands_orient_R1_prev "previous # other candidates of same orientation, round 1"

// Number of other candidates from same party
gen nb_othercands_party_R1=nb_cands_party_R1-1
label var nb_othercands_party_R1 "# other candidates of same party, round 1"

gen nb_othercands_party_R1_next=nb_cands_party_R1_next
replace nb_othercands_party_R1_next=nb_cands_party_R1_next-1 if party==party_next & running_indiv_next==1
label var nb_othercands_party_R1_next "next # other candidates of same party, round 1"

// Number of candidates from same orientation and not affiliated with same party
byso year election_type party: gen temp=orientation==orientation_next if party!="" & running_indiv_next==1 & party==party_next
byso year election_type party running_indiv_next: egen temp2=min(temp)
ta temp2 //parties that are unequivocally of the same orientation next time
gen temp3=temp2==1
byso year election_type party: egen same_orient=max(temp3)
drop temp*

gen nb_cands_diffparty_R1_next=nb_cands_orient_R1_next-nb_cands_party_R1_next if same_orient==1 
label var nb_cands_diffparty_R1_next "next # candidates of same orientation and not same party, round 1"

gen nb_othercands_diffparty_R1_next=nb_othercands_orient_R1_next-nb_othercands_party_R1_next if same_orient==1 
label var nb_othercands_diffparty_R1_next "next # other candidates of same orientation and not same party, round 1"

// Number of past runners and winenrs from same orientation
gen nb_otherrerun_orient_next=nb_rerun_orient_next
replace nb_otherrerun_orient_next=nb_rerun_orient_next-1 if orientation==orientation_next & running_indiv_next==1
label var nb_otherrerun_orient_next "next # other rerunners of same orientation"

gen nb_otherrerun2_orient_next=nb_rerun2_orient_next
replace nb_otherrerun2_orient_next=nb_rerun2_orient_next-1 if orientation==orientation_next & running_indiv_next==1 & running_indiv_prev==1
label var nb_otherrerun2_orient_next "next # other long-time rerunners of same orientation"

gen nb_otherwin2_orient_next=nb_win2_orient_next
replace nb_otherwin2_orient_next=nb_win2_orient_next-1 if orientation==orientation_next & running_indiv_next==1 & win_indiv_prev==1
label var nb_otherwin2_orient_next "next # other past winners of same orientation"

// Number of other candidates affiliated with a party
gen nb_other_party_orient=nb_party_orient if realparty_cand==0
replace nb_other_party_orient=nb_party_orient-1 if realparty_cand==1 //number of other candidates from a real party today
label var nb_other_party_orient "# other candidates of same orientation affiliated with a party"

gen nb_otherparty_orient_next=nb_party_orient_next
replace nb_otherparty_orient_next=nb_party_orient_next-1 if orientation==orientation_next & running_indiv_next==1 & realparty_cand_next==1
label var nb_otherparty_orient_next "next # other candidates of same orientation and not same party"

byso year election_type party: gen temp=realparty_cand==realparty_cand_next if party==party_next & party!="" & running_indiv_next==1
byso year election_type party running_indiv_next: egen temp2=min(temp)
ta temp2 
gen temp3=temp2==1
byso year election_type party: egen same_realparty=max(temp3)
ta same_realparty
drop temp*

gen nb_noparty_orient_next=nb_cands_orient_R1_next-nb_party_orient_next
label var nb_noparty_orient_next "next # candidates of same orientation not affiliated with a party"
gen nb_othernoparty_orient_next=nb_noparty_orient_next
replace nb_othernoparty_orient_next=nb_noparty_orient_next-1 if orientation==orientation_next & running_indiv_next==1 & realparty_cand_next==0
label var nb_noparty_orient_next "next # other candidates of same orientation not affiliated with a party"

assert nb_othernoparty_orient_next+nb_otherparty_orient_next==nb_othercands_orient_R1_next

// Generate number of OTHER candidates from same coalition
gen nb_othercands_coal_R1_next=nb_cands_coal_R1_next
replace nb_othercands_coal_R1_next=nb_cands_coal_R1_next-1 if coalition==coalition_next & running_indiv_next==1
label var nb_othercands_coal_R1_next "next # other candidates of same coalition, round 1"

gen nb_othercands_coal_R1_prev=nb_cands_coal_R1_prev
replace nb_othercands_coal_R1_prev=nb_cands_coal_R1_prev-1 if coalition==coalition_prev & running_indiv_prev==1
label var nb_othercands_coal_R1_prev "previous # other candidates of same coalition, round 1"

// Predicted assignment
gen treat=running>0

foreach level in "indiv" "party" "orient"{
foreach x of varlist gender_cand dummy_* realparty_cand running_indiv_prev votesh_indiv_R1_prev win_indiv_prev running_party_prev votesh_party_R1_prev win_party_prev running_orient_prev votesh_orient_R1_prev win_orient_prev nb_othercands_orient_R1_prev nb_othercands_orient_R1{
gen c_`x'=`x'
gen d_`x'=c_`x'==. //dummy for covariate to be missing, so that we can incldue all observations from main sample in regression
replace c_`x'=0 if c_`x'==.
}
reg treat d_* c_* if main_sample_`level'==1
predict treat_pred_`level' if main_sample_`level'==1
drop d_* c_*
label var treat_pred_`level' "Predicted treatment assignment"
}

label var treat "Treatment assignment"

// District code 
egen code_district=group(id_district)

// Drop variables that we do not need
drop  nb_cands_cont1_R1 running_cont1 nb_rerun_cont1 nb_rerun2_cont1 nb_win2_cont1 votes_cont1_R1 votesh_cont1_R1 win_cont1 nb_cands_cont1_R2 nb_qualif_cont1 secround_cont1 qualif_cont1 votes_cont1_R2 votesh_cont1_R2 winR1_cont1 winR2_cont1 nb_cands_cont2_R1 running_cont2 nb_rerun_cont2 nb_rerun2_cont2 nb_win2_cont2 votes_cont2_R1 votesh_cont2_R1 win_cont2 nb_cands_cont2_R2 nb_qualif_cont2 secround_cont2 qualif_cont2 votes_cont2_R2 votesh_cont2_R2 winR1_cont2 winR2_cont2 nb_cands_cont3_R1 running_cont3 nb_rerun_cont3 nb_rerun2_cont3 nb_win2_cont3 votes_cont3_R1 votesh_cont3_R1 win_cont3 nb_cands_cont3_R2 nb_qualif_cont3 secround_cont3 qualif_cont3 votes_cont3_R2 votesh_cont3_R2 winR1_cont3 winR2_cont3 nb_cands_cont1_R1_next running_cont1_next nb_rerun_cont1_next nb_rerun2_cont1_next nb_win2_cont1_next votes_cont1_R1_next votesh_cont1_R1_next win_cont1_next nb_cands_cont1_R2_next nb_qualif_cont1_next secround_cont1_next qualif_cont1_next votes_cont1_R2_next votesh_cont1_R2_next winR1_cont1_next winR2_cont1_next nb_cands_cont2_R1_next running_cont2_next nb_rerun_cont2_next nb_rerun2_cont2_next nb_win2_cont2_next votes_cont2_R1_next votesh_cont2_R1_next win_cont2_next nb_cands_cont2_R2_next nb_qualif_cont2_next secround_cont2_next qualif_cont2_next votes_cont2_R2_next votesh_cont2_R2_next winR1_cont2_next winR2_cont2_next nb_cands_cont3_R1_next running_cont3_next nb_rerun_cont3_next nb_rerun2_cont3_next nb_win2_cont3_next votes_cont3_R1_next votesh_cont3_R1_next win_cont3_next nb_cands_cont3_R2_next nb_qualif_cont3_next secround_cont3_next qualif_cont3_next votes_cont3_R2_next votesh_cont3_R2_next winR1_cont3_next winR2_cont3_next nb_cands_cont1_R1_prev running_cont1_prev nb_rerun_cont1_prev nb_rerun2_cont1_prev nb_win2_cont1_prev votes_cont1_R1_prev votesh_cont1_R1_prev win_cont1_prev nb_cands_cont1_R2_prev nb_qualif_cont1_prev secround_cont1_prev qualif_cont1_prev votes_cont1_R2_prev votesh_cont1_R2_prev winR1_cont1_prev winR2_cont1_prev nb_cands_cont2_R1_prev running_cont2_prev nb_rerun_cont2_prev nb_rerun2_cont2_prev nb_win2_cont2_prev votes_cont2_R1_prev votesh_cont2_R1_prev win_cont2_prev nb_cands_cont2_R2_prev nb_qualif_cont2_prev secround_cont2_prev qualif_cont2_prev votes_cont2_R2_prev votesh_cont2_R2_prev winR1_cont2_prev winR2_cont2_prev nb_cands_cont3_R1_prev running_cont3_prev nb_rerun_cont3_prev nb_rerun2_cont3_prev nb_win2_cont3_prev votes_cont3_R1_prev votesh_cont3_R1_prev win_cont3_prev nb_cands_cont3_R2_prev nb_qualif_cont3_prev secround_cont3_prev qualif_cont3_prev votes_cont3_R2_prev votesh_cont3_R2_prev winR1_cont3_prev winR2_cont3_prev nb_cands_cont1_R1_prev2 running_cont1_prev2 nb_rerun_cont1_prev2 nb_rerun2_cont1_prev2 nb_win2_cont1_prev2 votes_cont1_R1_prev2 votesh_cont1_R1_prev2 win_cont1_prev2 nb_cands_cont1_R2_prev2 nb_qualif_cont1_prev2 secround_cont1_prev2 qualif_cont1_prev2 votes_cont1_R2_prev2 votesh_cont1_R2_prev2 winR1_cont1_prev2 winR2_cont1_prev2 nb_cands_cont2_R1_prev2 running_cont2_prev2 nb_rerun_cont2_prev2 nb_rerun2_cont2_prev2 nb_win2_cont2_prev2 votes_cont2_R1_prev2 votesh_cont2_R1_prev2 win_cont2_prev2 nb_cands_cont2_R2_prev2 nb_qualif_cont2_prev2 secround_cont2_prev2 qualif_cont2_prev2 votes_cont2_R2_prev2 votesh_cont2_R2_prev2 winR1_cont2_prev2 winR2_cont2_prev2 nb_cands_cont3_R1_prev2 running_cont3_prev2 nb_rerun_cont3_prev2 nb_rerun2_cont3_prev2 nb_win2_cont3_prev2 votes_cont3_R1_prev2 votesh_cont3_R1_prev2 win_cont3_prev2 nb_cands_cont3_R2_prev2 nb_qualif_cont3_prev2 secround_cont3_prev2 qualif_cont3_prev2 votes_cont3_R2_prev2 votesh_cont3_R2_prev2 winR1_cont3_prev2 winR2_cont3_prev2 id_time id_unique_cand_next2  nb_run_cand nb_win_cand rerun_cand rerun2_cand win2_cand  lsi_party sim_party simtf_party lsibig_party simbig_party simbigtf_party  lsi_party_prev sim_party_prev simtf_party_prev lsibig_party_prev simbig_party_prev simbigtf_party_prev  lsi_party_prev2 sim_party_prev2 simtf_party_prev2 lsibig_party_prev2 simbig_party_prev2 simbigtf_party_prev2  orientation_prev2 realparty_cand_prev2 party_prev2  same_orient same_realparty id_unique_cand_next id_unique_cand_prev

// Save final database
save "$output/database_win", replace

***** 7B/ Merge with runoff treatment assignment *****

use "$temp/assignment_runoff", clear

// District level
merge m:1 election_type id_district id_time using "$temp/district"
gen nomerge_temp=_merge==2 //districts that do not satisfy the criteria of either runoff assignment 
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_next"
assert _merge!=2
count if (_merge==1 & nomerge_next!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_next==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev"
assert _merge!=2
count if (_merge==1 & nomerge_prev!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_prev==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev2"
assert _merge!=2
count if (_merge==1 & nomerge_prev2!=1 & nomerge_temp!=1) | (_merge!=1 & nomerge_prev2==1 & nomerge_temp!=1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

drop if nomerge_temp==1 //districts not usable for this identification strategy
drop nomerge_temp

// Orientation level
merge m:1 election_type id_district orientation id_time using "$temp/orient"
count if (_merge==1 & orientation!="") | (_merge!=1 & orientation=="")
assert r(N)==0 //candidates without a known political label
drop if _merge==2 //orientations that do no match with either candidate kept for this assignment
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_next"

local var nb_cands_orient_R1_next running_orient_next nb_cands_orient_R2_next nb_rerun_orient_next nb_rerun2_orient_next nb_win2_orient_next nb_party_orient_next nb_qualif_orient_next secround_orient_next qualif_orient_next win_orient_next votesh_orient_R1_next votes_orient_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

local var nb_cands_orient_R1_*_next running_orient_*_next win_orient_*_next votesh_orient_R1_*_next nb_party_orient_*_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev"
local var nb_cands_orient_R1_prev running_orient_prev nb_cands_orient_R2_prev nb_rerun_orient_prev nb_rerun2_orient_prev nb_win2_orient_prev nb_party_orient_prev nb_qualif_orient_prev secround_orient_prev qualif_orient_prev win_orient_prev votesh_orient_R1_prev votes_orient_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev2"
local var nb_cands_orient_R1_prev2 running_orient_prev2 nb_cands_orient_R2_prev2 nb_rerun_orient_prev2 nb_rerun2_orient_prev2 nb_win2_orient_prev2 nb_party_orient_prev2 nb_qualif_orient_prev2 secround_orient_prev2 qualif_orient_prev2 win_orient_prev2 votesh_orient_R1_prev2 votes_orient_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Party level
merge m:1 election_type id_district party id_time using "$temp/party"
count if (_merge==1 & party!="") | (_merge!=1 & party=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical party
drop if _merge==2 //parties that do no match with either candidate kept for this assignment
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_next"
local var nb_cands_party_R1_next running_party_next nb_cands_party_R2_next nb_rerun_party_next nb_rerun2_party_next nb_win2_party_next nb_qualif_party_next secround_party_next qualif_party_next win_party_next votesh_party_R1_next votes_party_R1_next ///
nb_cands_cont*_R1_next running_cont*_next nb_cands_cont*_R2_next nb_rerun_cont*_next nb_rerun2_cont*_next nb_win2_cont*_next nb_qualif_cont*_next secround_cont*_next qualif_cont*_next win_cont*_next votesh_cont*_R1_next votes_cont*_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

local var nb_cands_party_R1_*_next running_party_*_next win_party_*_next votesh_party_R1_*_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev"
local var nb_cands_party_R1_prev running_party_prev nb_cands_party_R2_prev nb_rerun_party_prev nb_rerun2_party_prev nb_win2_party_prev nb_qualif_party_prev secround_party_prev qualif_party_prev win_party_prev votesh_party_R1_prev votes_party_R1_prev ///
nb_cands_cont*_R1_prev running_cont*_prev nb_cands_cont*_R2_prev nb_rerun_cont*_prev nb_rerun2_cont*_prev nb_win2_cont*_prev nb_qualif_cont*_prev secround_cont*_prev qualif_cont*_prev win_cont*_prev votesh_cont*_R1_prev votes_cont*_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev2"
local var nb_cands_party_R1_prev2 running_party_prev2 nb_cands_party_R2_prev2 nb_rerun_party_prev2 nb_rerun2_party_prev2 nb_win2_party_prev2 nb_qualif_party_prev2 secround_party_prev2 qualif_party_prev2 win_party_prev2 votesh_party_R1_prev2 votes_party_R1_prev2 ///
nb_cands_cont*_R1_prev2 running_cont*_prev2 nb_cands_cont*_R2_prev2 nb_rerun_cont*_prev2 nb_rerun2_cont*_prev2 nb_win2_cont*_prev2 nb_qualif_cont*_prev2 secround_cont*_prev2 qualif_cont*_prev2 win_cont*_prev2 votesh_cont*_R1_prev2 votes_cont*_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Coalition level
merge m:1 election_type id_district coalition id_time using "$temp/coalition"
count if (_merge==1 & coalition!="") | (_merge!=1 & coalition=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical coalition
drop if _merge==2 //parties that do no match with either candidate kept for this assignment
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_next"
local var nb_cands_coal_R1_next running_coal_next nb_cands_coal_R2_next nb_rerun_coal_next nb_rerun2_coal_next nb_win2_coal_next nb_qualif_coal_next secround_coal_next qualif_coal_next win_coal_next votesh_coal_R1_next votes_coal_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev"
local var nb_cands_coal_R1_prev running_coal_prev nb_cands_coal_R2_prev nb_rerun_coal_prev nb_rerun2_coal_prev nb_win2_coal_prev nb_qualif_coal_prev secround_coal_prev qualif_coal_prev win_coal_prev votesh_coal_R1_prev votes_coal_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev2"
local var nb_cands_coal_R1_prev2 running_coal_prev2 nb_cands_coal_R2_prev2 nb_rerun_coal_prev2 nb_rerun2_coal_prev2 nb_win2_coal_prev2 nb_qualif_coal_prev2 secround_coal_prev2 qualif_coal_prev2 win_coal_prev2 votesh_coal_R1_prev2 votes_coal_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Individual level
merge m:1 id_unique_cand using "$temp/indiv"
assert _merge!=1
drop if _merge==2 //candidaes not retained in assignment dataset
drop _merge

merge m:1 id_unique_cand using "$temp/indiv_next"
local var running_indiv_next secround_indiv_next qualif_indiv_next win_indiv_next votesh_indiv_R1_next votes_indiv_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 id_unique_cand using "$temp/indiv_prev"
local var running_indiv_prev secround_indiv_prev qualif_indiv_prev win_indiv_prev votesh_indiv_R1_prev votes_indiv_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 id_unique_cand using "$temp/indiv_prev2"
local var running_indiv_prev2 secround_indiv_prev2 qualif_indiv_prev2 win_indiv_prev2 votesh_indiv_R1_prev2 votes_indiv_R1_prev2 
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Sample definition
gen main_sample_orient1=(assignment_type==1 & flag_secround_cand==0 & orientation!="nc" & nomerge_next==0 & flag_missing_orient_next==0)
gen main_sample_orient2=(assignment_type==2 & flag_secround_cand==0 & orientation!="nc" & flag_same_orient==0 & nomerge_next==0 & flag_missing_orient_next==0)
gen main_sample_orient3=(assignment_type==3 & flag_secround_cand==0 & orientation!="nc" & flag_same_orient==0 & nomerge_next==0 & flag_missing_orient_next==0)

forval i=1/3{
label var main_sample_orient`i' "main sample for orientation-level analysis with assignment `i'"
gen placebo_sample_orient`i'=(main_sample_orient`i'==1 & nomerge_prev==0 & flag_missing_orient_prev==0)
label var placebo_sample_orient`i' "placebo sample for orientation-level analysis with assignment `i'"
gen placebo2_sample_orient`i'=(main_sample_orient`i'==1 & nomerge_prev2==0 & flag_missing_orient_prev2==0)
label var placebo2_sample_orient`i' "placebo (t-2) sample for orientation-level analysis with assignment `i'"
}

gen main_sample_party1=(assignment_type==1 & flag_secround_cand==0 & party!="" & nomerge_next==0 & flag_missing_party_next==0)
gen main_sample_party2=(assignment_type==2 & flag_secround_cand==0 & party!="" & flag_same_party==0 & nomerge_next==0 & flag_missing_party_next==0)
gen main_sample_party3=(assignment_type==3 & flag_secround_cand==0 & party!="" & flag_same_party==0 & nomerge_next==0 & flag_missing_party_next==0)

forval i=1/3{
label var main_sample_party`i' "main sample for party-level analysis with assignment `i'"
gen placebo_sample_party`i'=(main_sample_party`i'==1 & nomerge_prev==0 & flag_missing_party_prev==0)
label var placebo_sample_party`i' "placebo sample for party-level analysis with assignment `i'"
gen placebo2_sample_party`i'=(main_sample_party`i'==1 & nomerge_prev2==0 & flag_missing_party_prev2==0)
label var placebo2_sample_party`i' "placebo (t-2) sample for party-level analysis with assignment `i'"
}

gen main_sample_coal1=(assignment_type==1 & flag_secround_cand==0 & coalition!=""& nomerge_next==0 & flag_missing_party_next==0) //NB: we use the same missing political_label condition as it is what we base our definition of coalition on
gen main_sample_coal2=(assignment_type==2 & flag_secround_cand==0 & coalition!="" & flag_same_coal==0 & nomerge_next==0 & flag_missing_party_next==0)
gen main_sample_coal3=(assignment_type==3 & flag_secround_cand==0 & coalition!="" & flag_same_coal==0 & nomerge_next==0 & flag_missing_party_next==0)

forval i=1/3{
label var main_sample_coal`i' "main sample for coalition-level analysis with assignment `i'"
gen placebo_sample_coal`i'=(main_sample_coal`i'==1 & nomerge_prev==0 & flag_missing_party_prev==0)
label var placebo_sample_coal`i' "placebo sample for coalition-level analysis with assignment `i'"
gen placebo2_sample_coal`i'=(main_sample_coal`i'==1 & nomerge_prev2==0 & flag_missing_party_prev2==0)
label var placebo2_sample_coal`i' "placebo (t-2) sample for coalition-level analysis with assignment `i'"
}

gen main_sample_indiv1=assignment_type==1 & flag_secround_cand==0 & nomerge_next==0
gen main_sample_indiv2=assignment_type==2 & flag_secround_cand==0 & nomerge_next==0
gen main_sample_indiv3=assignment_type==3 & flag_secround_cand==0 & nomerge_next==0
forval i=1/3{
gen placebo_sample_indiv`i'=(main_sample_indiv`i'==1 & nomerge_prev==0)
gen placebo2_sample_indiv`i'=(main_sample_indiv`i'==1 & nomerge_prev2==0)
}

forval i=1/3{
label var main_sample_indiv`i' "main sample for candidate-level analysis with assignment `i'"
label var placebo_sample_indiv`i' "placebo sample for candidate-level analysis with assignment `i'"
label var placebo2_sample_indiv`i' "placebo (t-2) sample for candidate-level analysis with assignment `i'"
}

gen flag_noparty_next=0
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
gen temp=flag_no_party_`y'_next==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_noparty_next=1 if temp2==1 & party=="`y'"
drop temp*
}
label var flag_noparty_next "next election without any candidate from the same party"

gen flag_noparty_prev=0
foreach y in "FDG" "FN" "MDM" "RG" "SOC" "UMP" "VEC"{
gen temp=flag_no_party_`y'_prev==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_noparty_prev=1 if temp2==1 & party=="`y'"
drop temp*
}
label var flag_noparty_prev "previous election without any candidate from the same party"

gen flag_nocoal_next=0
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
gen temp=flag_no_coal_`y'_next==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_nocoal_next=1 if temp2==1 & coalition=="`y'"
drop temp*
}
label var flag_nocoal_next "next election without any candidate from the same coalition"

gen flag_nocoal_prev=0
foreach y in "FR" "R" "C" "L" "FL" "VEC"{
gen temp=flag_no_coal_`y'_prev==1
byso year: egen temp2=max(temp)
ta temp2
replace flag_nocoal_prev=1 if temp2==1 & coalition=="`y'"
drop temp*
}
label var flag_nocoal_prev "previous election without any candidate from the same coalition"

// Add left/right + orientation dummies and incumbent majority vs others (at NEXT election)
foreach o in "fl" "l" "c" "r" "fr" "nc"{
gen dummy_`o'=orientation=="`o'" if orientation!=""
label var dummy_`o' "orientation is `o'"
}
gen left=orientation=="fl" | orientation=="l"  if orientation!=""
label var left "left or far-left"

gen samepres=0 if realparty_cand==1
replace samepres=1 if election_type==1 & year_next==1962 & party=="UMP"
replace samepres=1 if election_type==1 & year_next==1967 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1968 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1973 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1978 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1981 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1988 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1993 & party=="SOC"
replace samepres=1 if election_type==1 & year_next==1997 & (party=="UMP" | party=="UDF")
replace samepres=1 if election_type==1 & year_next==2002 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==1 & year_next==2007 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2012 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2017 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==1982 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1985 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1988 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1992 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1994 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==2 & year_next==1998 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2001 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2004 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2008 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2011 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2015 & (party=="SOC" | party=="RG")

label var samepres "belongs to next incumbent/current national majority"

// Generate number of OTHER candidates from same orientation
gen nb_othercands_orient_R1=nb_cands_orient_R1-1
label var nb_othercands_orient_R1 "# other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_next=nb_cands_orient_R1_next
replace nb_othercands_orient_R1_next=nb_cands_orient_R1_next-1 if orientation==orientation_next & running_indiv_next==1
label var nb_othercands_orient_R1_next "next # other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev
replace nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev-1 if orientation==orientation_prev & running_indiv_prev==1
label var nb_othercands_orient_R1_prev "previous # other candidates of same orientation, round 1"

// Number of other candidates from same party
gen nb_othercands_party_R1=nb_cands_party_R1-1
label var nb_othercands_party_R1 "# other candidates of same party, round 1"

gen nb_othercands_party_R1_next=nb_cands_party_R1_next
replace nb_othercands_party_R1_next=nb_cands_party_R1_next-1 if party==party_next & running_indiv_next==1
label var nb_othercands_party_R1_next "next # other candidates of same party, round 1"

// Number of candidates from same orientation and not affiliated with same party
byso year election_type party: gen temp=orientation==orientation_next if party!="" & running_indiv_next==1 & party==party_next
byso year election_type party running_indiv_next: egen temp2=min(temp)
ta temp2 //parties that are unequivocally of the same orientation next time
gen temp3=temp2==1
byso year election_type party: egen same_orient=max(temp3)
drop temp*

gen nb_cands_diffparty_R1_next=nb_cands_orient_R1_next-nb_cands_party_R1_next if same_orient==1 
label var nb_cands_diffparty_R1_next "next # candidates of same orientation and not same party, round 1"

gen nb_othercands_diffparty_R1_next=nb_othercands_orient_R1_next-nb_othercands_party_R1_next if same_orient==1 
label var nb_othercands_diffparty_R1_next "next # other candidates of same orientation and not same party, round 1"

// Predicted assignment
gen treat=running>0

foreach level in "indiv" "party" "orient"{
foreach x of varlist gender_cand dummy_* realparty_cand running_indiv_prev votesh_indiv_R1_prev win_indiv_prev running_party_prev votesh_party_R1_prev win_party_prev running_orient_prev votesh_orient_R1_prev win_orient_prev nb_othercands_orient_R1_prev nb_othercands_orient_R1{
gen c_`x'=`x'
gen d_`x'=c_`x'==. //dummy for covariate to be missing, so that we can incldue all observations from main sample in regression
replace c_`x'=0 if c_`x'==.
}
reg treat d_* c_* if main_sample_`level'3==1
predict treat_pred_`level' if main_sample_`level'3==1
drop d_* c_*
label var treat_pred_`level' "Predicted treatment assignment"
}

// District code 
egen code_district=group(id_district)

// Drop variables that we do not need
drop  nb_cands_cont1_R1 running_cont1 nb_rerun_cont1 nb_rerun2_cont1 nb_win2_cont1 votes_cont1_R1 votesh_cont1_R1 win_cont1 nb_cands_cont1_R2 nb_qualif_cont1 secround_cont1 qualif_cont1 votes_cont1_R2 votesh_cont1_R2 winR1_cont1 winR2_cont1 nb_cands_cont2_R1 running_cont2 nb_rerun_cont2 nb_rerun2_cont2 nb_win2_cont2 votes_cont2_R1 votesh_cont2_R1 win_cont2 nb_cands_cont2_R2 nb_qualif_cont2 secround_cont2 qualif_cont2 votes_cont2_R2 votesh_cont2_R2 winR1_cont2 winR2_cont2 nb_cands_cont3_R1 running_cont3 nb_rerun_cont3 nb_rerun2_cont3 nb_win2_cont3 votes_cont3_R1 votesh_cont3_R1 win_cont3 nb_cands_cont3_R2 nb_qualif_cont3 secround_cont3 qualif_cont3 votes_cont3_R2 votesh_cont3_R2 winR1_cont3 winR2_cont3 nb_cands_cont1_R1_next running_cont1_next nb_rerun_cont1_next nb_rerun2_cont1_next nb_win2_cont1_next votes_cont1_R1_next votesh_cont1_R1_next win_cont1_next nb_cands_cont1_R2_next nb_qualif_cont1_next secround_cont1_next qualif_cont1_next votes_cont1_R2_next votesh_cont1_R2_next winR1_cont1_next winR2_cont1_next nb_cands_cont2_R1_next running_cont2_next nb_rerun_cont2_next nb_rerun2_cont2_next nb_win2_cont2_next votes_cont2_R1_next votesh_cont2_R1_next win_cont2_next nb_cands_cont2_R2_next nb_qualif_cont2_next secround_cont2_next qualif_cont2_next votes_cont2_R2_next votesh_cont2_R2_next winR1_cont2_next winR2_cont2_next nb_cands_cont3_R1_next running_cont3_next nb_rerun_cont3_next nb_rerun2_cont3_next nb_win2_cont3_next votes_cont3_R1_next votesh_cont3_R1_next win_cont3_next nb_cands_cont3_R2_next nb_qualif_cont3_next secround_cont3_next qualif_cont3_next votes_cont3_R2_next votesh_cont3_R2_next winR1_cont3_next winR2_cont3_next nb_cands_cont1_R1_prev running_cont1_prev nb_rerun_cont1_prev nb_rerun2_cont1_prev nb_win2_cont1_prev votes_cont1_R1_prev votesh_cont1_R1_prev win_cont1_prev nb_cands_cont1_R2_prev nb_qualif_cont1_prev secround_cont1_prev qualif_cont1_prev votes_cont1_R2_prev votesh_cont1_R2_prev winR1_cont1_prev winR2_cont1_prev nb_cands_cont2_R1_prev running_cont2_prev nb_rerun_cont2_prev nb_rerun2_cont2_prev nb_win2_cont2_prev votes_cont2_R1_prev votesh_cont2_R1_prev win_cont2_prev nb_cands_cont2_R2_prev nb_qualif_cont2_prev secround_cont2_prev qualif_cont2_prev votes_cont2_R2_prev votesh_cont2_R2_prev winR1_cont2_prev winR2_cont2_prev nb_cands_cont3_R1_prev running_cont3_prev nb_rerun_cont3_prev nb_rerun2_cont3_prev nb_win2_cont3_prev votes_cont3_R1_prev votesh_cont3_R1_prev win_cont3_prev nb_cands_cont3_R2_prev nb_qualif_cont3_prev secround_cont3_prev qualif_cont3_prev votes_cont3_R2_prev votesh_cont3_R2_prev winR1_cont3_prev winR2_cont3_prev nb_cands_cont1_R1_prev2 running_cont1_prev2 nb_rerun_cont1_prev2 nb_rerun2_cont1_prev2 nb_win2_cont1_prev2 votes_cont1_R1_prev2 votesh_cont1_R1_prev2 win_cont1_prev2 nb_cands_cont1_R2_prev2 nb_qualif_cont1_prev2 secround_cont1_prev2 qualif_cont1_prev2 votes_cont1_R2_prev2 votesh_cont1_R2_prev2 winR1_cont1_prev2 winR2_cont1_prev2 nb_cands_cont2_R1_prev2 running_cont2_prev2 nb_rerun_cont2_prev2 nb_rerun2_cont2_prev2 nb_win2_cont2_prev2 votes_cont2_R1_prev2 votesh_cont2_R1_prev2 win_cont2_prev2 nb_cands_cont2_R2_prev2 nb_qualif_cont2_prev2 secround_cont2_prev2 qualif_cont2_prev2 votes_cont2_R2_prev2 votesh_cont2_R2_prev2 winR1_cont2_prev2 winR2_cont2_prev2 nb_cands_cont3_R1_prev2 running_cont3_prev2 nb_rerun_cont3_prev2 nb_rerun2_cont3_prev2 nb_win2_cont3_prev2 votes_cont3_R1_prev2 votesh_cont3_R1_prev2 win_cont3_prev2 nb_cands_cont3_R2_prev2 nb_qualif_cont3_prev2 secround_cont3_prev2 qualif_cont3_prev2 votes_cont3_R2_prev2 votesh_cont3_R2_prev2 winR1_cont3_prev2 winR2_cont3_prev2 id_time id_unique_cand_next2  nb_run_cand nb_win_cand rerun_cand rerun2_cand win2_cand  lsi_party sim_party simtf_party lsibig_party simbig_party simbigtf_party  lsi_party_prev sim_party_prev simtf_party_prev lsibig_party_prev simbig_party_prev simbigtf_party_prev  lsi_party_prev2 sim_party_prev2 simtf_party_prev2 lsibig_party_prev2 simbig_party_prev2 simbigtf_party_prev2  orientation_prev2 realparty_cand_prev2 party_prev2 treat id_unique_cand_next id_unique_cand_prev same_orient

// Save final database
save "$output/database_runoff", replace

***** 7C/ Merge with overall database *****
use "$intermediate/initial_database", clear

// District level
merge m:1 election_type id_district id_time using "$temp/district"
assert _merge!=2 
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_next"
assert _merge!=2
count if (_merge==1 & nomerge_next!=1) | (_merge!=1 & nomerge_next==1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev"
assert _merge!=2
count if (_merge==1 & nomerge_prev!=1) | (_merge!=1 & nomerge_prev==1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

merge m:1 election_type id_district id_time using "$temp/district_prev2"
assert _merge!=2
count if (_merge==1 & nomerge_prev2!=1) | (_merge!=1 & nomerge_prev2==1)
assert r(N)==0 //ok, all _merge==1 were expected
drop _merge

// Orientation level
merge m:1 election_type id_district orientation id_time using "$temp/orient"
count if (_merge==1 & orientation!="") | (_merge!=1 & orientation=="")
assert r(N)==0 //candidates without a known political label
assert _merge!=2 
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_next"
local var nb_cands_orient_R1_next running_orient_next nb_cands_orient_R2_next nb_rerun_orient_next nb_rerun2_orient_next nb_win2_orient_next nb_party_orient_next nb_qualif_orient_next secround_orient_next qualif_orient_next win_orient_next votesh_orient_R1_next votes_orient_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev"
local var nb_cands_orient_R1_prev running_orient_prev nb_cands_orient_R2_prev nb_rerun_orient_prev nb_rerun2_orient_prev nb_win2_orient_prev nb_party_orient_prev nb_qualif_orient_prev secround_orient_prev qualif_orient_prev win_orient_prev votesh_orient_R1_prev votes_orient_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district orientation id_time using "$temp/orient_prev2"
local var nb_cands_orient_R1_prev2 running_orient_prev2 nb_cands_orient_R2_prev2 nb_rerun_orient_prev2 nb_rerun2_orient_prev2 nb_win2_orient_prev2 nb_party_orient_prev2 nb_qualif_orient_prev2 secround_orient_prev2 qualif_orient_prev2 win_orient_prev2 votesh_orient_R1_prev2 votes_orient_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & orientation!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Party level
merge m:1 election_type id_district party id_time using "$temp/party"
count if (_merge==1 & party!="") | (_merge!=1 & party=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical party
assert _merge!=2 
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_next"
local var nb_cands_party_R1_next running_party_next nb_cands_party_R2_next nb_rerun_party_next nb_rerun2_party_next nb_win2_party_next nb_qualif_party_next secround_party_next qualif_party_next win_party_next votesh_party_R1_next votes_party_R1_next ///
nb_cands_cont*_R1_next running_cont*_next nb_cands_cont*_R2_next nb_rerun_cont*_next nb_rerun2_cont*_next nb_win2_cont*_next nb_qualif_cont*_next secround_cont*_next qualif_cont*_next win_cont*_next votesh_cont*_R1_next votes_cont*_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev"
local var nb_cands_party_R1_prev running_party_prev nb_cands_party_R2_prev nb_rerun_party_prev nb_rerun2_party_prev nb_win2_party_prev nb_qualif_party_prev secround_party_prev qualif_party_prev win_party_prev votesh_party_R1_prev votes_party_R1_prev ///
nb_cands_cont*_R1_prev running_cont*_prev nb_cands_cont*_R2_prev nb_rerun_cont*_prev nb_rerun2_cont*_prev nb_win2_cont*_prev nb_qualif_cont*_prev secround_cont*_prev qualif_cont*_prev win_cont*_prev votesh_cont*_R1_prev votes_cont*_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district party id_time using "$temp/party_prev2"
local var nb_cands_party_R1_prev2 running_party_prev2 nb_cands_party_R2_prev2 nb_rerun_party_prev2 nb_rerun2_party_prev2 nb_win2_party_prev2 nb_qualif_party_prev2 secround_party_prev2 qualif_party_prev2 win_party_prev2 votesh_party_R1_prev2 votes_party_R1_prev2 ///
nb_cands_cont*_R1_prev2 running_cont*_prev2 nb_cands_cont*_R2_prev2 nb_rerun_cont*_prev2 nb_rerun2_cont*_prev2 nb_win2_cont*_prev2 nb_qualif_cont*_prev2 secround_cont*_prev2 qualif_cont*_prev2 win_cont*_prev2 votesh_cont*_R1_prev2 votes_cont*_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & party!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Coalition level
merge m:1 election_type id_district coalition id_time using "$temp/coalition"
count if (_merge==1 & coalition!="") | (_merge!=1 & coalition=="")
assert r(N)==0 //candidates without a known political label or not part of a main historical party
assert _merge!=2 
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_next"
local var nb_cands_coal_R1_next running_coal_next nb_cands_coal_R2_next nb_rerun_coal_next nb_rerun2_coal_next nb_win2_coal_next nb_qualif_coal_next secround_coal_next qualif_coal_next win_coal_next votesh_coal_R1_next votes_coal_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev"
local var nb_cands_coal_R1_prev running_coal_prev nb_cands_coal_R2_prev nb_rerun_coal_prev nb_rerun2_coal_prev nb_win2_coal_prev nb_qualif_coal_prev secround_coal_prev qualif_coal_prev win_coal_prev votesh_coal_R1_prev votes_coal_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge m:1 election_type id_district coalition id_time using "$temp/coalition_prev2"
local var nb_cands_coal_R1_prev2 running_coal_prev2 nb_cands_coal_R2_prev2 nb_rerun_coal_prev2 nb_rerun2_coal_prev2 nb_win2_coal_prev2 nb_qualif_coal_prev2 secround_coal_prev2 qualif_coal_prev2 win_coal_prev2 votesh_coal_R1_prev2 votes_coal_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 & coalition!="" //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Individual level
merge 1:1 id_unique_cand using "$temp/indiv"
assert _merge==3
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_next"
local var running_indiv_next secround_indiv_next qualif_indiv_next win_indiv_next votesh_indiv_R1_next votes_indiv_R1_next
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_next==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_prev"
local var running_indiv_prev secround_indiv_prev qualif_indiv_prev win_indiv_prev votesh_indiv_R1_prev votes_indiv_R1_prev
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

merge 1:1 id_unique_cand using "$temp/indiv_prev2"
local var running_indiv_prev2 secround_indiv_prev2 qualif_indiv_prev2 win_indiv_prev2 votesh_indiv_R1_prev2 votes_indiv_R1_prev2
foreach x of varlist `var'{
replace `x'=0 if _merge==1 & nomerge_prev2==0 //change missing values in zero if not merged but should be included in analysis
}

drop if _merge==2
drop _merge

// Sample definition
gen main_sample_orient=(orientation!="nc" & nomerge_next==0 & flag_missing_orient_next==0)
ta main_sample_orient 
label var main_sample_orient "main sample for orientation-level analysis"
gen main_sample_party=(party!="" & nomerge_next==0 & flag_missing_party_next==0)
ta main_sample_party 
label var main_sample_party "main sample for party-level analysis"
gen main_sample_coal=(coalition!="" & nomerge_next==0 & flag_missing_party_next==0) //NB: we use the same missing political_label condition as it is what we base our definition of coalition on
ta main_sample_coal
label var main_sample_coal "main sample for coalition-level analysis"
gen main_sample_indiv=nomerge_next==0
label var main_sample_indiv "main sample for candidate-level analysis"

// Add left/right + orientation dummies and incumbent majority vs others (at NEXT election)
foreach o in "fl" "l" "c" "r" "fr" "nc"{
gen dummy_`o'=orientation=="`o'" if orientation!=""
label var dummy_`o' "orientation is `o'"
}
gen left=orientation=="fl" | orientation=="l"  if orientation!=""
label var left "left or far-left"

gen samepres=0 if realparty_cand==1
replace samepres=1 if election_type==1 & year_next==1962 & party=="UMP"
replace samepres=1 if election_type==1 & year_next==1967 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1968 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1973 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==1978 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1981 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1988 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==1 & year_next==1993 & party=="SOC"
replace samepres=1 if election_type==1 & year_next==1997 & (party=="UMP" | party=="UDF")
replace samepres=1 if election_type==1 & year_next==2002 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==1 & year_next==2007 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2012 & (party=="UMP")
replace samepres=1 if election_type==1 & year_next==2017 & (party=="SOC" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==1982 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1985 & (party=="SOC" | party=="FDG" | party=="RG")
replace samepres=1 if election_type==2 & year_next==1988 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1992 & (party=="SOC")
replace samepres=1 if election_type==2 & year_next==1994 & (party=="UMP" | party=="MDM")
replace samepres=1 if election_type==2 & year_next==1998 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2001 & (party=="SOC" | party=="FDG" | party=="RG" | party=="VEC")
replace samepres=1 if election_type==2 & year_next==2004 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2008 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2011 & (party=="UMP")
replace samepres=1 if election_type==2 & year_next==2015 & (party=="SOC" | party=="RG")

label var samepres "belongs to next incumbent/current national majority"

// Generate number of OTHER candidates from same orientation
gen nb_othercands_orient_R1=nb_cands_orient_R1-1
label var nb_othercands_orient_R1 "# other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_next=nb_cands_orient_R1_next
replace nb_othercands_orient_R1_next=nb_cands_orient_R1_next-1 if orientation==orientation_next & running_indiv_next==1
label var nb_othercands_orient_R1_next "next # other candidates of same orientation, round 1"

gen nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev
replace nb_othercands_orient_R1_prev=nb_cands_orient_R1_prev-1 if orientation==orientation_prev & running_indiv_prev==1
label var nb_othercands_orient_R1_prev "previous # other candidates of same orientation, round 1"

// District code 
egen code_district=group(id_district)

// Drop variables that we do not need
drop  nb_cands_cont1_R1 running_cont1 nb_rerun_cont1 nb_rerun2_cont1 nb_win2_cont1 votes_cont1_R1 votesh_cont1_R1 win_cont1 nb_cands_cont1_R2 nb_qualif_cont1 secround_cont1 qualif_cont1 votes_cont1_R2 votesh_cont1_R2 winR1_cont1 winR2_cont1 nb_cands_cont2_R1 running_cont2 nb_rerun_cont2 nb_rerun2_cont2 nb_win2_cont2 votes_cont2_R1 votesh_cont2_R1 win_cont2 nb_cands_cont2_R2 nb_qualif_cont2 secround_cont2 qualif_cont2 votes_cont2_R2 votesh_cont2_R2 winR1_cont2 winR2_cont2 nb_cands_cont3_R1 running_cont3 nb_rerun_cont3 nb_rerun2_cont3 nb_win2_cont3 votes_cont3_R1 votesh_cont3_R1 win_cont3 nb_cands_cont3_R2 nb_qualif_cont3 secround_cont3 qualif_cont3 votes_cont3_R2 votesh_cont3_R2 winR1_cont3 winR2_cont3 nb_cands_cont1_R1_next running_cont1_next nb_rerun_cont1_next nb_rerun2_cont1_next nb_win2_cont1_next votes_cont1_R1_next votesh_cont1_R1_next win_cont1_next nb_cands_cont1_R2_next nb_qualif_cont1_next secround_cont1_next qualif_cont1_next votes_cont1_R2_next votesh_cont1_R2_next winR1_cont1_next winR2_cont1_next nb_cands_cont2_R1_next running_cont2_next nb_rerun_cont2_next nb_rerun2_cont2_next nb_win2_cont2_next votes_cont2_R1_next votesh_cont2_R1_next win_cont2_next nb_cands_cont2_R2_next nb_qualif_cont2_next secround_cont2_next qualif_cont2_next votes_cont2_R2_next votesh_cont2_R2_next winR1_cont2_next winR2_cont2_next nb_cands_cont3_R1_next running_cont3_next nb_rerun_cont3_next nb_rerun2_cont3_next nb_win2_cont3_next votes_cont3_R1_next votesh_cont3_R1_next win_cont3_next nb_cands_cont3_R2_next nb_qualif_cont3_next secround_cont3_next qualif_cont3_next votes_cont3_R2_next votesh_cont3_R2_next winR1_cont3_next winR2_cont3_next nb_cands_cont1_R1_prev running_cont1_prev nb_rerun_cont1_prev nb_rerun2_cont1_prev nb_win2_cont1_prev votes_cont1_R1_prev votesh_cont1_R1_prev win_cont1_prev nb_cands_cont1_R2_prev nb_qualif_cont1_prev secround_cont1_prev qualif_cont1_prev votes_cont1_R2_prev votesh_cont1_R2_prev winR1_cont1_prev winR2_cont1_prev nb_cands_cont2_R1_prev running_cont2_prev nb_rerun_cont2_prev nb_rerun2_cont2_prev nb_win2_cont2_prev votes_cont2_R1_prev votesh_cont2_R1_prev win_cont2_prev nb_cands_cont2_R2_prev nb_qualif_cont2_prev secround_cont2_prev qualif_cont2_prev votes_cont2_R2_prev votesh_cont2_R2_prev winR1_cont2_prev winR2_cont2_prev nb_cands_cont3_R1_prev running_cont3_prev nb_rerun_cont3_prev nb_rerun2_cont3_prev nb_win2_cont3_prev votes_cont3_R1_prev votesh_cont3_R1_prev win_cont3_prev nb_cands_cont3_R2_prev nb_qualif_cont3_prev secround_cont3_prev qualif_cont3_prev votes_cont3_R2_prev votesh_cont3_R2_prev winR1_cont3_prev winR2_cont3_prev nb_cands_cont1_R1_prev2 running_cont1_prev2 nb_rerun_cont1_prev2 nb_rerun2_cont1_prev2 nb_win2_cont1_prev2 votes_cont1_R1_prev2 votesh_cont1_R1_prev2 win_cont1_prev2 nb_cands_cont1_R2_prev2 nb_qualif_cont1_prev2 secround_cont1_prev2 qualif_cont1_prev2 votes_cont1_R2_prev2 votesh_cont1_R2_prev2 winR1_cont1_prev2 winR2_cont1_prev2 nb_cands_cont2_R1_prev2 running_cont2_prev2 nb_rerun_cont2_prev2 nb_rerun2_cont2_prev2 nb_win2_cont2_prev2 votes_cont2_R1_prev2 votesh_cont2_R1_prev2 win_cont2_prev2 nb_cands_cont2_R2_prev2 nb_qualif_cont2_prev2 secround_cont2_prev2 qualif_cont2_prev2 votes_cont2_R2_prev2 votesh_cont2_R2_prev2 winR1_cont2_prev2 winR2_cont2_prev2 nb_cands_cont3_R1_prev2 running_cont3_prev2 nb_rerun_cont3_prev2 nb_rerun2_cont3_prev2 nb_win2_cont3_prev2 votes_cont3_R1_prev2 votesh_cont3_R1_prev2 win_cont3_prev2 nb_cands_cont3_R2_prev2 nb_qualif_cont3_prev2 secround_cont3_prev2 qualif_cont3_prev2 votes_cont3_R2_prev2 votesh_cont3_R2_prev2 winR1_cont3_prev2 winR2_cont3_prev2 id_time id_unique_cand_next2  nb_run_cand nb_win_cand rerun_cand rerun2_cand win2_cand  lsi_party sim_party simtf_party lsibig_party simbig_party simbigtf_party  lsi_party_prev sim_party_prev simtf_party_prev lsibig_party_prev simbig_party_prev simbigtf_party_prev  lsi_party_prev2 sim_party_prev2 simtf_party_prev2 lsibig_party_prev2 simbig_party_prev2 simbigtf_party_prev2  orientation_prev2 realparty_cand_prev2 party_prev2 id_unique_cand_next id_unique_cand_prev candidate

// Save final database
save "$output/database_all", replace


